ImagePickerKMP — New Extensions & Image Compression
Repository: ismoy/ImagePickerKMP
Overview
ImagePickerKMP now introduces powerful new extension functions and image compression features for more flexible image handling across Android, iOS and Web platforms.
Available Extension Functions
These new extensions allow you to easily convert captured or selected images into different formats — perfect for storage, UI display, graphics, or network use.
| Function | Returns | Usage |
loadBytes() | ByteArray | For file operations and storage |
loadPainter() | Painter | For direct display in Compose UI |
loadImageBitmap() | ImageBitmap | For Compose graphics operations |
loadBase64() | String | For API calls and network transmission |
Example Usage
// State variables
var showCamera by remember { mutableStateOf(false) }
var showGallery by remember { mutableStateOf(false) }
var capturedImage by remember { mutableStateOf<Painter?>(null) }
var selectedPhotos by remember { mutableStateOf<List<GalleryPhotoHandler.PhotoResult>>(emptyList()) }
// Camera with extension functions
if (showCamera) {
ImagePickerLauncher(
config = ImagePickerConfig(
onPhotoCaptured = { result ->
// Use extension functions to get different formats
val imageBytes = result.loadBytes() // For file operations
val imagePainter = result.loadPainter() // For UI display
val imageBitmap = result.loadImageBitmap() // For graphics operations
val imageBase64 = result.loadBase64() // For API calls
// Store the painter to display later
capturedImage = imagePainter
showCamera = false
},
onError = { showCamera = false },
onDismiss = { showCamera = false },
directCameraLaunch = true
)
)
}
// Gallery with extension functions
if (showGallery) {
GalleryPickerLauncher(
onPhotosSelected = { photos ->
selectedPhotos = photos
showGallery = false
},
onError = { showGallery = false },
onDismiss = { showGallery = false },
allowMultiple = true
)
}
// Display captured image
capturedImage?.let { painter ->
Image(
painter = painter,
contentDescription = "Captured photo",
modifier = Modifier.size(200.dp)
)
}
// Display selected photos
LazyColumn {
items(selectedPhotos) { photo ->
photo.loadPainter()?.let { painter ->
Image(
painter = painter,
contentDescription = "Selected photo",
modifier = Modifier
.fillMaxWidth()
.height(200.dp)
)
}
}
}
gist: read More