我的问题得到了很好的回答:
Jake Bloom: Using Share Sheets on iOS and Android
import Foundation
import SwiftUI
import LinkPresentation
class ShareImage: UIActivityItemProvider {
var image: UIImage
override var item: Any {
get {
return self.image
}
}
override init(placeholderItem: Any) {
guard let image = placeholderItem as? UIImage else {
fatalError("Couldn't create image from provided item")
}
self.image = image
super.init(placeholderItem: placeholderItem)
}
@available(iOS 13.0, *)
override func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
metadata.title = "Result Image"
var thumbnail: NSSecureCoding = NSNull()
if let imageData = self.image.pngData() {
thumbnail = NSData(data: imageData)
}
metadata.imageProvider = NSItemProvider(item: thumbnail, typeIdentifier: "public.png")
return metadata
}
}
进而:
func actionSheet(image: UIImage?) {
guard let data = image else { return }
let item = ShareImage(placeholderItem: data)
let activityViewController = UIActivityViewController(activityItems: [item], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(activityViewController, animated: true, completion: nil)
}