Easy to use image cropping library for Kotlin and Compose Multiplatform, with support for shapes, aspect-ratios, transformations, large images, auto zoom...
Version:
Compatibility:
Krop version | Kotlin version | Compose version |
---|---|---|
0.1.4+ | 2.0 | 1.7 |
0.1.2 | 2.0 | 1.6 |
0.1.0 | 1.9 | 1.6 |
Option 1: Add the ui
module to use the crop dialog out of the box:
commonMain.dependencies {
implementation("com.attafitamim.krop:ui:$version")
}
Option 2: If you are looking for a custom design, use the core
module instead:
commonMain.dependencies {
implementation("com.attafitamim.krop:core:$version")
}
For hints on how to use core
logic for a custom design, check sources of the ui
module.
val imageCropper = rememberImageCropper()
class MyViewModel : ViewModel {
val imageCropper = ImageCropper()
}
scope.launch {
val result = imageCropper.crop(bitmap) // Suspends until user accepts or cancels cropping
when (result) {
CropResult.Cancelled -> { }
is CropError -> { }
is CropResult.Success -> { result.bitmap }
}
}
val cropState = imageCropper.cropState
if(cropState != null) ImageCropperDialog(state = cropState)
That's it !
The crop
function provides overloads for ImageBitmap
, Uri
, File
, but it is also possible to use a custom ImageSrc
.
You can use the rememberImagePicker
function to easily pick an image and crop it :
val scope = rememberCoroutineScope()
val context = LocalContext.current
val imagePicker = rememberImagePicker(onImage = { uri ->
scope.launch {
val result = imageCropper.crop(uri, context)
}
})
To customize the ui of the image cropper you can provide a different implementation of CropperStyle
to the cropper dialog.
You can also use the cropperStyle
factory function. example :
ImageCropperDialog(
state = cropState,
style = cropperStyle(
overlay = Color.Red.copy(alpha = .5f),
autoZoom = false,
guidelines = null,
)
)