Skip to content

Commit

Permalink
Create iOS temp file in file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedRejeb committed May 29, 2024
1 parent 69ea560 commit c9e1171
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ private fun rememberImagePickerLauncher(
picker: PHPickerViewController,
didFinishPicking: List<*>,
) {
picker.dismissViewControllerAnimated(true, null)
println("didFinishPicking: $didFinishPicking")
didFinishPicking.forEach {
val result = it as? PHPickerResult ?: return@forEach
result.itemProvider.loadFileRepresentationForTypeIdentifier(
typeIdentifier = UTTypeImage.identifier,
) { url, error ->
if (error != null) {
println("Error: $error")
return@loadFileRepresentationForTypeIdentifier
}
onResult(listOfNotNull(url?.let(::KmpFile)))

onResult(listOfNotNull(url?.createTempFile()?.let(::KmpFile)))
}
}

picker.dismissViewControllerAnimated(true, null)
}
}
}
Expand All @@ -140,6 +140,7 @@ private fun rememberImagePickerLauncher(
delegate = pickerDelegate,
selectionMode = selectionMode,
)

UIApplication.sharedApplication.keyWindow?.rootViewController?.presentViewController(
imagePicker,
true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mohamedrejeb.calf.picker

import platform.Foundation.NSData
import platform.Foundation.NSTemporaryDirectory
import platform.Foundation.NSURL
import platform.Foundation.NSUUID
import platform.Foundation.dataWithContentsOfURL
import platform.Foundation.writeToURL

internal fun NSURL.createTempFile(): NSURL? {
val extension = absoluteString?.substringAfterLast('.', "") ?: return null
val data = NSData.dataWithContentsOfURL(this) ?: return null
return NSURL.fileURLWithPath("${NSTemporaryDirectory()}/${NSUUID().UUIDString}.$extension").apply {
data.writeToURL(this, true)
}
}

0 comments on commit c9e1171

Please sign in to comment.