Swift Share Extension无法加载图像

时间:2018-10-14 19:43:18

标签: ios swift flutter ios12 share-extension

以下是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)
                    }
                }
            }
        }
    }
}

共享扩展名的文本部分可以正常工作,但是如果我尝试将图像发送到应用程序。我得到这个回应。

Screenshot

注意:

  • iOS 11.4上进行测试时,相同的代码可以正常运行
  • 我已经在iPhone 6S模拟器iOS 12.0上进行了测试,但失败了。

1 个答案:

答案 0 :(得分:-1)

您是否尝试过将数据直接转换为UIImage?