SwiftUI:创建 UIImage 快照并将其用作 shareSheet 中的缩略图
SwiftUI: Create a UIImage snapshot and use it as thumbnail in a shareSheet
我在 iOS 应用程序中实现快照共享,因此 UIImage 是从视图创建的,它在共享表中并且工作正常,但我无法将此 UIImage 显示为缩略图是ShareSheet。
func actionSheet(image: UIImage) {
let urlShare = image
let activityVC = UIActivityViewController(activityItems: [urlShare], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
}
UIImage 是即时创建的,因此在应用程序中没有 url
我怎样才能让它显示出来?
我的问题得到了很好的回答:
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)
}
我在 iOS 应用程序中实现快照共享,因此 UIImage 是从视图创建的,它在共享表中并且工作正常,但我无法将此 UIImage 显示为缩略图是ShareSheet。
func actionSheet(image: UIImage) {
let urlShare = image
let activityVC = UIActivityViewController(activityItems: [urlShare], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
}
UIImage 是即时创建的,因此在应用程序中没有 url
我怎样才能让它显示出来?
我的问题得到了很好的回答: 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)
}