Browse Source

[iOS/ImageViewer] Proposal of Fix share image button infinite loop (#4973)

Proposal of a Fixes ( Unfortunately, I can't test the rest of the code
as the iOS camera requires a real device. )

Fixes https://github.com/JetBrains/compose-multiplatform/issues/4970
pull/3640/merge v1.7.0-dev1698
issamux 5 months ago committed by GitHub
parent
commit
0bd1536fc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 33
      examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/ImageViewer.ios.kt
  2. 15
      examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/storage/IosImageStorage.ios.kt

33
examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/ImageViewer.ios.kt

@ -52,22 +52,23 @@ fun getDependencies(ioScope: CoroutineScope, toastState: MutableState<ToastState
override val sharePicture: SharePicture = object : SharePicture { override val sharePicture: SharePicture = object : SharePicture {
override fun share(context: PlatformContext, picture: PictureData) { override fun share(context: PlatformContext, picture: PictureData) {
ioScope.launch { ioScope.launch {
val data = imageStorage.getNSDataToShare(picture) imageStorage.getNSURLToShare(picture).path?.let { imageUrl ->
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
val window = UIApplication.sharedApplication.windows.last() as? UIWindow val window = UIApplication.sharedApplication.windows.last() as? UIWindow
val currentViewController = window?.rootViewController val currentViewController = window?.rootViewController
val activityViewController = UIActivityViewController( val activityViewController = UIActivityViewController(
activityItems = listOf( activityItems = listOf(
UIImage(data = data), UIImage.imageWithContentsOfFile(imageUrl),
picture.description picture.description
), ),
applicationActivities = null applicationActivities = null
) )
currentViewController?.presentViewController( currentViewController?.presentViewController(
viewControllerToPresent = activityViewController, viewControllerToPresent = activityViewController,
animated = true, animated = true,
completion = null, completion = null,
) )
}
} }
} }
} }

15
examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/storage/IosImageStorage.ios.kt

@ -119,6 +119,21 @@ class IosImageStorage(
} }
}.readData() }.readData()
} }
suspend fun getNSURLToShare(picture: PictureData): NSURL = withContext(Dispatchers.IO) {
when (picture) {
is PictureData.Camera -> {
picture.jpgFile
}
is PictureData.Resource -> {
NSURL(
fileURLWithPath = NSBundle.mainBundle.resourcePath + "/" + picture.resource,
isDirectory = false
)
}
}
}
} }
@OptIn(ExperimentalForeignApi::class) @OptIn(ExperimentalForeignApi::class)

Loading…
Cancel
Save