From 0bd1536fc7e3b9635221533f88710b0c3d01193d Mon Sep 17 00:00:00 2001 From: issamux Date: Mon, 24 Jun 2024 12:34:28 -0400 Subject: [PATCH] [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 --- .../example/imageviewer/ImageViewer.ios.kt | 33 ++++++++++--------- .../storage/IosImageStorage.ios.kt | 15 +++++++++ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/ImageViewer.ios.kt b/examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/ImageViewer.ios.kt index 1f40b9740ac..c42b9949872 100755 --- a/examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/ImageViewer.ios.kt +++ b/examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/ImageViewer.ios.kt @@ -52,22 +52,23 @@ fun getDependencies(ioScope: CoroutineScope, toastState: MutableState + 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, + ) + } } } } diff --git a/examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/storage/IosImageStorage.ios.kt b/examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/storage/IosImageStorage.ios.kt index b84d46d13c0..732c4676139 100644 --- a/examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/storage/IosImageStorage.ios.kt +++ b/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)