Translations: 简体中文
Fetcher is used to get data from uri, return FetchResult, and hand it over to Decoder for use.
Each uri supported by Sketch has a corresponding Fetcher implementation, as shown in the following table:
URI | Fetcher | Create | Dependent modules | Android | iOS | Desktop | Web |
---|---|---|---|---|---|---|---|
http://, https:// | HttpUriFetcher | - | - | ✅ | ✅ | ✅ | ✅ |
file://, / | FileUriFetcher | newFileUri() | - | ✅ | ✅ | ✅ | ✅ |
file:///compose_resource/ | ComposeResourceUriFetcher | newComposeResourceUri() | sketch-compose-resources | ✅ | ✅ | ✅ | ✅ |
data:image/, data:img/ | Base64UriFetcher | newBase64Uri() | - | ✅ | ✅ | ✅ | ✅ |
file:///android_asset/ | AssetUriFetcher | newAssetUri() | - | ✅ | ❌ | ❌ | ❌ |
content:// | ContentUriFetcher | - | - | ✅ | ❌ | ❌ | ❌ |
android.resource:// | ResourceUriFetcher | newResourceUri() | - | ✅ | ❌ | ❌ | ❌ |
app.icon:// | AppIconUriFetcher | newAppIconUri() | sketch-extensions-core | ✅ | ❌ | ❌ | ❌ |
file:///kotlin_resource/ | KotlinResourceUriFetcher | newKotlinResourceUri() | - | ❌ | ✅ | ✅ | ❌ |
- AssetUriFetcher is used to load images from the Android assets directory
- ContentUriFetcher ContentResolver for Android to load images
- ResourceUriFetcher is used to load images from Android's resources directory
- AppIconUriFetcher is used to load the icon of the installed App. It also needs to rely
on
sketch-extensions-core
module. Learn more - Base64UriFetcher is used to load images from the base64 data block of the uri itself
- ComposeResourceUriFetcher is used to load images from the composeResources directory of
Compose Multiplatform, it also needs to depend on the
sketch-compose-resources
module. - KotlinResourceUriFetcher is used to load images from the resources directory of kotlin
Fetcher that needs to rely on a separate module (such as ComposeResourceUriFetcher) needs to be registered when initializing Sketch, as follows:
// Register for all ImageRequests when customizing Sketch
Sketch.Builder(context).apply {
components {
addFetcher(ComposeResourceUriFetcher.Factory())
}
}.build()
// Register for a single ImageRequest when loading an image
ImageRequest(context, "https://www.example.com/image.gif") {
components {
addFetcher(ComposeResourceUriFetcher.Factory())
}
}
First implement the Fetcher interface to define your Fetcher and its Factory, and then register it through the addFetcher() method, as follows:
class MyFetcher : Fetcher {
override suspend fun fetch(): Result<FetchResult> {
// Parse your uri here and get the data
}
companion object {
const val MY_SCHEME = "myUri"
}
class Factory : Fetcher.Factory {
override fun create(sketch: Sketch, request: ImageRequest): MyFetcher? {
return if (request.uri.startWith("$MY_SCHEME://")) {
MyFetcher()
} else {
null
}
}
}
}
// Register for all ImageRequests when customizing Sketch
Sketch.Builder(context).apply {
components {
addFetcher(MyFetcher.Factory())
}
}.build()
// Register for a single ImageRequest when loading an image
ImageRequest(context, "myUri://sample.jpeg") {
components {
addFetcher(MyFetcher.Factory())
}
}