使用“操作扩展”导入图像 - URL到本地图像可以使用但不能使用实际图像数据

时间:2017-02-15 07:05:40

标签: ios iphone swift3 uiimagepngrepresentation

我的iOS应用程序(Swift 3)需要使用Action Extension从其他应用程序获取重要图像。我正在使用标准的Action Extension模板代码,该代码适用于iOS Mail和Photos等应用程序,其中共享的图像是本地文件的URL。但对于某些应用程序,其中共享的图像是实际的图像数据本身,我的动作扩展代码没有得到图像。

  for item: Any in self.extensionContext!.inputItems {
        let inputItem = item as! NSExtensionItem
        for provider: Any in inputItem.attachments! {

            let itemProvider = provider as! NSItemProvider

            if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {  //we'll take any image type: gif, png, jpg, etc

        // This is an image. We'll load it, then place it in our image view.
        weak var weakImageView = self.imageView
        itemProvider.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil, completionHandler: { (imageURL,

            error) in
            OperationQueue.main.addOperation {


                if let strongImageView = weakImageView {


                    if let imageURL = imageURL as? NSURL {

                        strongImageView.image = UIImage(data: NSData(contentsOf: imageURL as URL)! as Data)

                        let imageData = NSData(contentsOf: imageURL as URL)! as Data
                        self.gifImageView.image = UIImage.gif(data: imageData)

                        let width = strongImageView.image?.size.width
                        let height = strongImageView.image?.size.height
                        ....  my custom logic

                   }
              }  

作为参考,我向开发人员联系了其中一个不能正常工作的应用程序,并且他分享了关于他如何将图像共享到Action Extension的代码。

//Here is the relevant code. At this point the scaledImage variable holds a UIImage. 

var activityItems = Array<Any?>()

if let pngData = UIImagePNGRepresentation(scaledImage) {

    activityItems.append(pngData)

} else {

    activityItems.append(scaledImage)
}

//Then a little later it presents the share sheet:

let activityVC = UIActivityViewController(activityItems: activityItems,applicationActivities: [])

self.present(activityVC, animated: true, completion: nil)              

1 个答案:

答案 0 :(得分:1)

感谢这篇文章,它很好地解释了挑战https://pspdfkit.com/blog/2017/action-extension/。总之,我们不知道共享应用程序是否为我们提供了现有图像的URL或仅提供原始图像数据,因此我们需要修改开箱即用的操作扩展模板代码以处理这两种情况。

for item: Any in self.extensionContext!.inputItems {
    let inputItem = item as! NSExtensionItem
    for provider: Any in inputItem.attachments! {

        let itemProvider = provider as! NSItemProvider

        if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {  //we'll take any image type: gif, png, jpg, etc

    // This is an image. We'll load it, then place it in our image view.
    weak var weakImageView = self.imageView
    itemProvider.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil, completionHandler: { (imageURL,

        error) in
        OperationQueue.main.addOperation {


            if let strongImageView = weakImageView {


                if let imageURL = imageURL as? NSURL {

                    strongImageView.image = UIImage(data: NSData(contentsOf: imageURL as URL)! as Data)

                    let imageData = NSData(contentsOf: imageURL as URL)! as Data
                    self.gifImageView.image = UIImage.gif(data: imageData)

                    let width = strongImageView.image?.size.width
                    let height = strongImageView.image?.size.height
                    ....  my custom logic

               }

               else 

                    guard let imageData = imageURL as? Data else { return } //can we cast to image data?
                    strongImageView_.image = UIImage(data: imageData_)

                    //custom logic

          }