commonMain.com.github.panpf.sketch.SingletonAsyncImagePainter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sketch-compose Show documentation
Show all versions of sketch-compose Show documentation
Android Sketch Image Loader
The newest version!
/*
* Copyright (C) 2024 panpf
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.panpf.sketch
import androidx.compose.foundation.Image
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.runtime.Composable
import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.graphics.drawscope.DrawScope.Companion.DefaultFilterQuality
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.Constraints
import com.github.panpf.sketch.request.ImageRequest
/**
* Return an [AsyncImagePainter] that executes an [ImageRequest] asynchronously and renders the result.
*
* **This is a lower-level API than [AsyncImage] and may not work as expected in all situations. **
*
* - [AsyncImagePainter] will not finish loading if [AsyncImagePainter.onDraw] is not called.
* This can occur if a composable has an unbounded (i.e. [Constraints.Infinity]) width/height
* constraint. For example, to use [AsyncImagePainter] with [LazyRow] or [LazyColumn], you must
* set a bounded width or height respectively using `Modifier.width` or `Modifier.height`.
* - [AsyncImageState.painterState] will not transition to [PainterState.Success] synchronously during the
* composition phase. Use [SubcomposeAsyncImage] or set a custom [ImageRequest.Builder.size] value
* (e.g. `size(Size(100, 100))`) if you need this.
*
* @param uri [ImageRequest.uri] value.
* @param state [AsyncImageState] that will be used to store the state of the request.
* @param contentScale Used to determine the aspect ratio scaling to be used if the canvas bounds
* are a different size from the intrinsic size of the image loaded by [uri]. This should be set
* to the same value that's passed to [Image].
* @param filterQuality Sampling algorithm applied to a bitmap when it is scaled and drawn into the
* destination.
*
* @see com.github.panpf.sketch.compose.common.test.SingletonAsyncImagePainterTest.testRememberAsyncImagePainter
*/
@Composable
@NonRestartableComposable
fun rememberAsyncImagePainter(
uri: String?,
state: AsyncImageState = rememberAsyncImageState(),
contentScale: ContentScale = ContentScale.Fit,
filterQuality: FilterQuality = DefaultFilterQuality,
): AsyncImagePainter = rememberAsyncImagePainter(
uri = uri,
sketch = SingletonSketch.get(LocalPlatformContext.current),
state = state,
contentScale = contentScale,
filterQuality = filterQuality
)
/**
* Return an [AsyncImagePainter] that executes an [ImageRequest] asynchronously and renders the result.
*
* **This is a lower-level API than [AsyncImage] and may not work as expected in all situations. **
*
* - [AsyncImagePainter] will not finish loading if [AsyncImagePainter.onDraw] is not called.
* This can occur if a composable has an unbounded (i.e. [Constraints.Infinity]) width/height
* constraint. For example, to use [AsyncImagePainter] with [LazyRow] or [LazyColumn], you must
* set a bounded width or height respectively using `Modifier.width` or `Modifier.height`.
* - [AsyncImageState.painterState] will not transition to [PainterState.Success] synchronously during the
* composition phase. Use [SubcomposeAsyncImage] or set a custom [ImageRequest.Builder.size] value
* (e.g. `size(Size(100, 100))`) if you need this.
*
* @param request [ImageRequest].
* @param state [AsyncImageState] that will be used to store the state of the request.
* @param contentScale Used to determine the aspect ratio scaling to be used if the canvas bounds
* are a different size from the intrinsic size of the image loaded by [request]. This should be set
* to the same value that's passed to [Image].
* @param filterQuality Sampling algorithm applied to a bitmap when it is scaled and drawn into the
* destination.
*
* @see com.github.panpf.sketch.compose.common.test.SingletonAsyncImagePainterTest.testRememberAsyncImagePainter2
*/
@Composable
@NonRestartableComposable
fun rememberAsyncImagePainter(
request: ImageRequest,
state: AsyncImageState = rememberAsyncImageState(),
contentScale: ContentScale = ContentScale.Fit,
filterQuality: FilterQuality = DefaultFilterQuality,
): AsyncImagePainter = rememberAsyncImagePainter(
request = request,
sketch = SingletonSketch.get(LocalPlatformContext.current),
state = state,
contentScale = contentScale,
filterQuality = filterQuality
)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy