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 fun share(context: PlatformContext, picture: PictureData) {
ioScope.launch {
val data = imageStorage.getNSDataToShare(picture)
withContext(Dispatchers.Main) {
val window = UIApplication.sharedApplication.windows.last() as? UIWindow
val currentViewController = window?.rootViewController
val activityViewController = UIActivityViewController(
activityItems = listOf(
UIImage(data = data),
picture.description
),
applicationActivities = null
)
currentViewController?.presentViewController(
viewControllerToPresent = activityViewController,
animated = true,
completion = null,
)
imageStorage.getNSURLToShare(picture).path?.let { imageUrl ->
withContext(Dispatchers.Main) {
val window = UIApplication.sharedApplication.windows.last() as? UIWindow
val currentViewController = window?.rootViewController
val activityViewController = UIActivityViewController(
activityItems = listOf(
UIImage.imageWithContentsOfFile(imageUrl),
picture.description
),
applicationActivities = null
)
currentViewController?.presentViewController(
viewControllerToPresent = activityViewController,
animated = true,
completion = null,
)
}
}
}
}

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

@ -119,6 +119,21 @@ class IosImageStorage(
}
}.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)

Loading…
Cancel
Save