Skip to content

Latest commit

 

History

History
42 lines (35 loc) · 1.32 KB

load_request.md

File metadata and controls

42 lines (35 loc) · 1.32 KB

LoadRequest

Translations: 简体中文

Use LoadRequest to load an image and obtain a Bitmap, as follows:

LoadRequest(context, "https://www.sample.com/image.jpg") {
    listener(
        onSuccess = { request: LoadRequest, result: LoadResult.Success ->
            val bitmap = result.bitmap
            // ...
        },
        onError = { request: LoadRequest, result: LoadResult.Error ->
            val throwable: Throwable = result.throwable
            // ...
        }
    )
}.enqueue()

When you need to obtain the loading results synchronously, you can use the execute method, as follows:

coroutineScope.launch(Dispatchers.Main) {
    val result: LoadResult = LoadRequest(context, "https://www.sample.com/image.jpg").execute()
    if (result is LoadResult.Success) {
        val bitmap = result.bitmap
        // ...
    } else if (result is LoadResult.Error) {
        val throwable: Throwable = result.throwable
        // ...
    }
}

Note: LoadRequest will not obtain the Bitmap from the memory cache, nor will it put the obtained Bitmap into the memory cache, because the Bitmap returned by LoadRequest is completely handed over to the user and is not controlled by Sketch.