以下是iOS
应用的flutter
部分的共享扩展中的代码。
我在XCode方面有丰富的工作经验,因此请原谅我的新手错误。
这是我的viewDidLoad
实现中的SLComposeServiceViewController
方法
override func viewDidLoad() {
super.viewDidLoad();
let content = self.extensionContext!.inputItems[0] as! NSExtensionItem;
let contentTypeImage = kUTTypeImage as String;
let contentTypeText = kUTTypeText as String;
for attachment in content.attachments as! [NSItemProvider] {
if attachment.hasItemConformingToTypeIdentifier(contentTypeImage) {
// Verify that the content type is image.
attachment.loadItem(forTypeIdentifier: contentTypeImage, options: nil) {
data, error in if error == nil {
let url = data as! NSURL
if let imageData = NSData(contentsOf: url as URL) {
let image = UIImage(data: imageData as Data)
// Do something with the image.
if(thingGoWrong) {
//show error message.
self.showErrorMessage(text: "Failed to read image.")
return
}
if (finalOperationSucceeded) {
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
}
}
} else {
// Display error dialog for not supported content. Though we should never receive any such thing.
self.showErrorMessage(text: error?.localizedDescription)
}
}
}
if attachment.hasItemConformingToTypeIdentifier(contentTypeText) {
attachment.loadItem(forTypeIdentifier: contentTypeText, options: nil) {
data, error in if error == nil {
let text = data as! String
// do something with the text
if (textOpSucceeded) {
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
}
}
}
}
}
}
共享扩展名的文本部分可以正常工作,但是如果我尝试将图像发送到应用程序。我得到这个回应。
iOS 11.4
上进行测试时,相同的代码可以正常运行iPhone 6S
模拟器iOS 12.0
上进行了测试,但失败了。答案 0 :(得分:-1)
您是否尝试过将数据直接转换为UIImage?