Alex
08/11/2024, 11:14 AMAlex
08/11/2024, 11:17 AMexpect/actual
implementation to produce a State
for the ImageRequest
(Coil 3.x.x), on Android I will just do the following:
@Composable
actual fun GalleryImage.imageRequest(): ImageRequest? =
ImageRequest.Builder(LocalPlatformContext.current)
.data(identifier)
.build()
identifier
is the images Uri
Alex
08/11/2024, 11:21 AM@OptIn(ExperimentalForeignApi::class)
@Composable
actual fun GalleryImage.imageRequest(): ImageRequest? {
val context = LocalPlatformContext.current
val request by produceState<ImageRequest?>(null) {
val imageObject = PHAsset.fetchAssetsWithLocalIdentifiers(listOf(identifier), null)
.firstObject()
val result = imageObject as? PHAsset ?: return@produceState
val imageManager = PHImageManager.defaultManager()
imageManager.requestImageForAsset(
asset = result,
targetSize = CGSizeMake(920.0, 1080.0),
contentMode = PHImageContentModeDefault,
options = null
) { image, _ ->
image?.toByteArray(80.0)?.let {
value = ImageRequest.Builder(context)
.data(it)
.build()
}
}
}
return request
}
On iOS
the identifier
is a `PHPickerResult`s assetIdentifier
, which comes from the iOS image picker
This works but has a couple of drawbacks:
1. I need to specify the image size to load in pixels, which does not take any layouting into consideration (I can definitely hack this in there with BoxWithConstraints
or something similar, but it feels wrong)
2. There is a couple conversions from identifier -> UIImage -> ByteArray, which I don’t know if its needlessly wasteful or might even lead to OOMs for many images?!
3. Threading is a mystery to me here, maybe its good, maybe its not, I cannot tell
Is there a better way on iOS for achieving this?
So far an iPhone 15 emulator does just fine displaying these images in a LazyColumn
, but this might just be because its running on an m1 which has insane hardware resources.Haley Cunningham
09/18/2024, 11:12 PMrequestImageForAssetWithIdentifier()
, and is quite buggy 😕Alex
09/19/2024, 7:41 AMHaley Cunningham
09/27/2024, 12:49 AMHaley Cunningham
09/27/2024, 12:49 AM