IOS - Swift 3 Share Image Preview

I am currently creating a share extension that accepts urls. As part of this, I configured my shared screen as described in the previous question to create a full screen controller. This all works great. However, in the default sharing composer view, I noticed a preview was shown on the web page. I'm trying to access this in my extension, but I can't seem to figure it out.

Specifically, I tried to use the method

loadPreviewImage

https://developer.apple.com/reference/foundation/nsitemprovider/1403925-loadpreviewimage

In the docs, you will notice that for the completion handler

states the following:

completion Handler A completion handler block to execute with the results. The first parameter of this block must be a parameter of type NSData, NSURL, UIImage (on iOS), or NSImage (on macOS) to get the image data. For more information about block implementation, see Completion Handler.

However, if I try to set this as a UIImage in my completion block, I get the error

Cannot convert value of type '(UIImage, _) -> ()' to expected argument type 'NSItemProvider.CompletionHandler!'

Sample code where itemProvider is confirmed to be an NSItemProvider instance using warnings

itemProvider.loadPreviewImage(options: nil) { (image: UIImage, error) in    
        }

      

The docs for the completion handler say to set this type to what you want and it will try to coerce the data to the specified type. Has anyone seen this before? I'm not sure what to do here as I cannot see what I am doing wrong.

https://developer.apple.com/reference/foundation/nsitemprovider/completionhandler

If all else fails, I'll take a look at how to use Javascript to get the image from the dom, but I would love the preview image that Apple seemed to provide

+3


source to share


1 answer


I don't know why the code in

itemProvider.loadPreviewImage(options: nil) { (image: UIImage, error) in    
        }

      

is not called when the Mail button is pressed.

My round way is saving the preview image in the method



override func configurationItems() -> [Any]! {
}

      

and

let inputItem: NSExtensionItem = self.extensionContext?.inputItems[0] as! NSExtensionItem
        let itemProvider = inputItem.attachments![0] as! NSItemProvider
if (itemProvider.hasItemConformingToTypeIdentifier("public.url")) {
            itemProvider.loadPreviewImage(options: nil, completionHandler: { (item, error) in // 画像を取得する
                if let image = item as? UIImage {
                    if let data = UIImagePNGRepresentation(image) {
                        self.photoNSURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("preview.png") as NSURL!

                        do {
                            try data.write(to: self.photoNSURL as URL, options: .atomic)
                        } catch {
                            print("\(#file)#\(#function)(\(#line)): error: \(error.localizedDescription)")
                        }
                    }
                }
            })
        }

      

+1


source







All Articles