Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Swift 3 Share Extension Preview Image

I'm currently building a share extension that accepts URLs. As part of this I've customised my share screen as outlined in a previous question to create a full screen view controller. This is all working fine. However in the default share composer view I noticed there was a preview image of the web page. I'm trying to access this in my extension but I can't seem to get hold of it.

Specifically I've been trying to use the method

loadPreviewImage

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

You will note in the docs that this says the following for the completion handler

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 (in iOS), or NSImage (in macOS) for receiving the image data. For more information about implementing the block, see Completion​Handler.

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

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

example code where itemProvider is confirmed to be an instance of NSItemProvider via guard statements

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

The docs for the completion Handler say to set this to what type you want and it will attempt to coerce the data to the type you specify. Has anyone seen this before? I'm not sure what to do here as I can't see what I'm doing wrong.

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

If all else fails I'll look at using some Javascript to get an image from the dom but I would have liked the preview image that Apple seemed to provide

like image 334
TommyBs Avatar asked Oct 17 '22 14:10

TommyBs


1 Answers

I don't know why the code in

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

not called when Post button tapped.

My round way is saving the preview image in method

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

as

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)")
                        }
                    }
                }
            })
        }
like image 130
Wenxu Zhang Avatar answered Nov 15 '22 05:11

Wenxu Zhang