Skip to content

WannaverseOfficial/kmp-image-selector

Repository files navigation

Wannaverse Logo

Build iOS Build Linux

Image Selector

A Kotlin Multiplatform image selection library for Android, iOS and Desktop.

Features

  • Selecting images on Android, iOS and Desktop
  • Setting aspect ratios

✅ Supported Platform

Platform Supported
Android ✔️
iOS ✔️
Desktop ✔️
Web ❌️

🚀 Installation

See the releases section of this repository for the latest version.

To your build.gradle under commonMain.dependencies add:

implementation "com.wannaverse:imageselector:<version>"

Important this library uses native code. You will need the additional dependencies depending on the targets you are building for:

Android under androidMain.dependencies: implementation("com.wannaverse:imageselector-android:")

iOS (ARM) under iosMain.dependencies: implementation("com.wannaverse:imageselector-iosarm64:")

iOS x64 under iosMain.dependencies: implementation("com.wannaverse:imageselector-iosx64:")

jvm: implementation("com.wannaverse:imageselector-jvm:")

Usage

KDocs

Below is a sample code that you may use.

When setting up the image selector on Android, you will need to add the following to your MainActivity's onCreate function:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    
    // Add these to register the image selector
    setImageSelectorActivity(this)
    registerImageSelectorLauncher()
}

Example ViewModel:

class AppViewModel : ViewModel() {
    val image = mutableStateOf<ImageData?>(null)

    fun chooseImage() = viewModelScope.launch {
        image.value = selectImage()
    }
}

Example Composable:

@Composable
fun App(viewModel: AppViewModel = viewModel { AppViewModel() }) {
    val image = remember { viewModel.image }
    val bitmap = image.value?.bytes?.toImageBitmap()
    
    Column(modifier = Modifier
        .fillMaxSize()
        .safeContentPadding()
    ) {
        Button(onClick = {
            viewModel.chooseImage()
        }) {
            Text(
                text = "Select Image"
            )
        }
        bitmap?.let {
            Image(
                bitmap = it,
                contentDescription = null
            )
        }
    }
}

📄 License

MIT LICENSE. See LICENSE for details.

🙌 Contributing

Pull requests and feature requests are welcome! If you encounter any issues, feel free to open an issue.