Skip to content

Commit

Permalink
feat(ios): Handle share cache image (UIImage) (#290)
Browse files Browse the repository at this point in the history
Add missing handler for cached image (UIImage) on iOS. That support receive images which could be shared from some chat app like messenger, zalo, whatsapp,... and also screenshot images.
  • Loading branch information
huynhmytuandev authored Apr 29, 2024
1 parent 4e0d3bb commit 9038445
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ios/Classes/RSIShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ open class RSIShareViewController: SLComposeServiceViewController {
index: index,
content: content)
}
else if let image = data as? UIImage {
this.handleMedia(forUIImage: image,
type: type,
index: index,
content: content)
}
}
}
break
Expand Down Expand Up @@ -121,6 +127,23 @@ open class RSIShareViewController: SLComposeServiceViewController {
}
}
}

private func handleMedia(forUIImage image: UIImage, type: SharedMediaType, index: Int, content: NSExtensionItem){
let tempPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupId)!.appendingPathComponent("TempImage.png")
if self.writeTempFile(image, to: tempPath) {
let newPathDecoded = tempPath.absoluteString.removingPercentEncoding!
sharedMedia.append(SharedMediaFile(
path: newPathDecoded,
mimeType: type == .image ? "image/png": nil,
type: type
))
}
if index == (content.attachments?.count ?? 0) - 1 {
if shouldAutoRedirect() {
saveAndRedirect()
}
}
}

private func handleMedia(forFile url: URL, type: SharedMediaType, index: Int, content: NSExtensionItem) {
let fileName = getFileName(from: url, type: type)
Expand Down Expand Up @@ -212,6 +235,20 @@ open class RSIShareViewController: SLComposeServiceViewController {
}
return name
}

private func writeTempFile(_ image: UIImage, to dstURL: URL) -> Bool {
do {
if FileManager.default.fileExists(atPath: dstURL.path) {
try FileManager.default.removeItem(at: dstURL)
}
let pngData = image.pngData();
try pngData?.write(to: dstURL);
return true;
} catch (let error){
print("Cannot write to temp file: \(error)");
return false;
}
}

private func copyFile(at srcURL: URL, to dstURL: URL) -> Bool {
do {
Expand Down

0 comments on commit 9038445

Please sign in to comment.