Coil Integration
Load and display images from PlatformFile using Coil
Introduction
FileKit provides seamless integration with Coil, a popular image loading library for Kotlin Multiplatform. This integration allows you to easily load and display images from PlatformFile
objects using Coil’s powerful image loading capabilities.
Configuration
Add the FileKit Coil dependency to your project:
Then, configure Coil in your app’s root composable:
This setup utilizes the addPlatformFileSupport()
extension function to register the necessary components within Coil’s pipeline, enabling it to handle PlatformFile
objects:
PlatformFileMapper
: Maps thePlatformFile
object to a data type that Coil’s fetchers can understand.PlatformFileFetcher
: Handles the actual loading of the image data from thePlatformFile
.PlatformFileKeyer
: Generates stable cache keys forPlatformFile
objects, allowing Coil to effectively cache the loaded images.
Usage
Once configured, you can use Coil’s standard AsyncImage
composable with PlatformFile
objects:
For more information about using Coil’s image loading capabilities, refer to the official Coil documentation.
Handling Security-Scoped Resources (iOS/macOS)
On iOS and macOS, if you obtain a PlatformFile
through mechanisms like the document picker, your app gets temporary, scoped access permission. To ensure Coil can load the image data while this permission is active, FileKit provides the securelyAccessFile
extension function for AsyncImagePainter.State
.
Use this function within the onState
callback of AsyncImage
:
This function automatically calls startAccessingSecurityScopedResource()
when Coil starts loading (State.Loading
) and stopAccessingSecurityScopedResource()
when it finishes (State.Success
or State.Error
), ensuring the file remains accessible throughout the loading process.
Example
Here’s a complete example showing how to use the Coil integration in a gallery: