我正在尝试从网站上获取缩略图,以便将其粘贴到我的自定义 UIViewController 上以进行共享扩展。我知道 SLComposeServiceViewController 这样做是免费的,但我必须制作一个自定义的视图控制器。有没有办法用现有的 API 做到这一点?
谢谢。
我正在尝试从网站上获取缩略图,以便将其粘贴到我的自定义 UIViewController 上以进行共享扩展。我知道 SLComposeServiceViewController 这样做是免费的,但我必须制作一个自定义的视图控制器。有没有办法用现有的 API 做到这一点?
谢谢。
我也达到了自定义的限制,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。
试试这个代码,从文件 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];