3

I'm trying to grab thumbnail image from a website so I can paste it on my custom UIViewController for share extension. I know SLComposeServiceViewController does this for free, but I have to make a customized view controller. Is there any way to do this with existing APIs?

Thanks.

Genki
  • 3,055
  • 2
  • 29
  • 42

2 Answers2

2

I also hit the limit in customizing SLComposeServiceViewController and had to create own preview. Basic approach is like this:

for (NSExtensionItem *item in self.extensionContext.inputItems)
{
    for (NSItemProvider *itemProvider in item.attachments)
    {
        //kUTTypeVCard, kUTTypeURL, kUTTypeImage, kUTTypeQuickTimeMovie
        NSString *typeIdentifier = (__bridge NSString *)kUTTypeImage;

        if ([itemProvider hasItemConformingToTypeIdentifier:typeIdentifier])
        {
            [itemProvider loadPreviewImageWithOptions:nil completionHandler:^(UIImage *image, NSError *error)
             {
                 if (image)
                 {
                     //Use image
                 }
             }];
        }
    }
}

Please note that

- (void)loadPreviewImageWithOptions:(NSDictionary *)options completionHandler:(NSItemProviderCompletionHandler)completionHandler

Loads the preview image for this item by either calling the supplied preview block or falling back to a QuickLook-based handler. This method, like loadItemForTypeIdentifier:options:completionHandler:, supports implicit type coercion for the item parameter of the completion block. Allowed value classes are: NSData, NSURL, UIImage/NSImage.

Nikita Ivaniushchenko
  • 1,425
  • 11
  • 11
  • 1
    loadPreviewImageWithOptions is Not loading image for kUTTypeURL. returns nil in image. – Ali Asad Jan 28 '16 at 22:06
  • it does work for me, loadPreviewImageWithOptions:option, pass the dictionary of options. NSValue *value = [NSValue valueWithCGSize:CGSizeMake(120, 120)]; NSDictionary * optionDict = [NSDictionary dictionaryWithObjectsAndKeys:value,NSItemProviderPreferredImageSizeKey, nil]; – Sagar In May 24 '16 at 05:31
  • I'm also having issue to have the loadPreviewImageWithOptions load a preview image when a URL is shared from Safari. The image is always nil. Does it still work for you? – cdf1982 Jun 28 '20 at 19:38
-1

Try this code, to get a thumbnail from file URL:

NSURL *path = self.url; 
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kQLThumbnailOptionIconModeKey];
CGImageRef ref = QLThumbnailImageCreate(kCFAllocatorDefault, (__bridge CFURLRef)path, CGSizeMake(600, 800 /* Or whatever size you want */), (__bridge CFDictionaryRef)options);
NSImage *thunbnail = [[NSImage alloc]initWithCGImage:ref size:NSZeroSize];
ThorstenC
  • 1,264
  • 11
  • 26
  • Oh, I just see that you are looking for a thumbnail from a website while my solutions handles with file URLs. Maybe the QLThumbnailImageCreate can also deal with website thumbnails. – ThorstenC Nov 13 '14 at 13:10