简体   繁体   中英

iOS 8 Share Extension Safari Image

I have set up a share extension in my iOS 8 app and everything is working fine. I can share from Photo app or from Safari. But when I am in Safari, I have no idea how to get the generated thumbnail image of the web page. I have registered the proper NSExtensionActivationRule's. When I share a photo from the Photo app, the NSItemProvider object type says it is public.jpeg and I am able to use

[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error)
{
         [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}];

in order to get the image

But when I share from Safari, the NSItemProvider type says it is public.url, and I have no idea how to get the image? I know how to get the url, by doing loadItemForTypeIdentifier:@"public.url" but how do I get the image?

Within the share extension, you can set up a javascript preprocessor to access the web page, and return information such as preview image.

Details for setting up the js preprocessor are here: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW12

I'm able to get the generated thumbnail when sharing web page, though unable to modify the size of it.

To get the image automatically generated on the share extension while sharing on Safari, use loadPreviewImageWithOptions:completionHandler:previewImageHandler.

[itemProvider loadPreviewImageWithOptions:nil completionHandler:^(UIImage *image, NSError *error){

   if(image){
        //do anything here with the image
   }

}

I'm able to retrieve the thumbnail auto-generated while sharing on Safari, but I'm unable to change the size of the image using:

NSString * const NSItemProviderPreferredImageSizeKey;

Reference: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSItemProvider_Class/

Do check out and see if that helps you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM