3

我正在尝试从网站上获取缩略图,以便将其粘贴到我的自定义 UIViewController 上以进行共享扩展。我知道 SLComposeServiceViewController 这样做是免费的,但我必须制作一个自定义的视图控制器。有没有办法用现有的 API 做到这一点?

谢谢。

4

2 回答 2

2

我也达到了自定义的限制,SLComposeServiceViewController不得不创建自己的预览。基本方法是这样的:

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
                 }
             }];
        }
    }
}

请注意

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

通过调用提供的预览块或回退到基于 QuickLook 的处理程序来加载此项的预览图像。此方法与 loadItemForTypeIdentifier:options:completionHandler: 类似,支持对完成块的 item 参数进行隐式类型强制。允许的值类是:NSData、NSURL、UIImage/NSImage。

于 2015-06-12T15:56:29.237 回答
-1

试试这个代码,从文件 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];
于 2014-11-12T14:55:51.103 回答