Supported on Android, iOS, macOS, JVM, JS and WASM targets
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 fromPlatformFile
objects using Coil’s powerful image loading capabilities.
Configuration
Add the FileKit Coil dependency to your project: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 standardAsyncImage
composable with PlatformFile
objects:
Handling Security-Scoped Resources (iOS/macOS)
On iOS and macOS, if you obtain aPlatformFile
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
:
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.