Skip to content

Commit

Permalink
[iOS/ImageViewer] Proposal of Fix share image button infinite loop (#…
Browse files Browse the repository at this point in the history
…4973)

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

Fixes #4970
  • Loading branch information
issamux authored Jun 24, 2024
1 parent b501e0f commit 0bd1536
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0bd1536

Please sign in to comment.