Image utilities
Utilities for working with images in Kotlin Multiplatform
FileKit Core provides powerful utilities for working with images across platforms. These utilities enable common image operations like compression, resizing, and saving to the device gallery, with a consistent API across all supported platforms.
Compressing Images
You can compress images to reduce their file size while maintaining acceptable quality:
The compressImage
function provides several key features:
- Quality control: Reduces the image quality based on the
quality
parameter (0-100) - Resizing: Resizes the image if
maxWidth
ormaxHeight
is specified, maintaining the aspect ratio - Format conversion: Converts the image to the specified format (JPEG or PNG)
- Metadata handling: On Android, automatically handles EXIF orientation metadata
Saving Images to Gallery
FileKit provides a way to save images to the device’s photo gallery:
The behavior of saveImageToGallery
varies slightly by platform:
Platform | Behavior |
---|---|
Android | Saves to the device’s Pictures directory and makes it visible in the gallery app |
iOS | Saves to the device’s Camera Roll |
macOS/JVM | Saves to the user’s Pictures directory |
JS/WASM | Not supported (no-op) |
Encoding and Converting Images
FileKit provides utilities for encoding ImageBitmap
objects into byte arrays and converting PlatformFile
objects into ImageBitmap
instances. These methods are useful for image manipulation and storage across platforms.
Encoding an ImageBitmap to ByteArray
The ImageBitmap.encodeToByteArray
method allows you to encode an ImageBitmap
into a ByteArray
using a specified format and quality.
Converting a PlatformFile to ImageBitmap
The PlatformFile.toImageBitmap
method converts a file into an ImageBitmap
for use in Compose UI or other image operations.
Example: Creating a Thumbnail
This function creates a thumbnail version of an image by resizing it to a maximum dimension while maintaining the aspect ratio.