NSItem提供照片格式,如uiimage或alasset

时间:2014-09-04 16:27:21

标签: ios8

如何从NSItemProvider文件中获取图像?当我使用-loadItemForTypeIdentifier:kUTTypeImage时,我得到一个看起来像这样的NSURL

file:///var/mobile/Media/DCIM/103APPLE/IMG_3349.JPG

我能用它做什么?谢谢!

TAGS:NSItemProvider NSExtensionItem ios共享扩展

5 个答案:

答案 0 :(得分:5)

只需使用

[imageItemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(id item, NSError *error) {
        if (item)
        {
            NSData *data = [NSData dataWithContentsOfURL:item];
        }

用数据做任何你想做的事

答案 1 :(得分:1)

我意识到我实际上可以做到这一点

[imageItemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(NSData * item, NSError *error) {
if (item)
{
    //do anything with item
}

答案 2 :(得分:0)

这是另一种选择,具体取决于您对图像的处理方式:

[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage
options:nil completionHandler:^(UIImage *image, NSError *error) {

                     NSData* data = UIImageJPEGRepresentation( image, 1.0f )];

                 }];

因此,此选项将其作为图像提供给您,然后您可以将其转换为NSData(如果您想要或将其保留为图像并显示它),不知道哪种方法会获得最佳性能!

答案 3 :(得分:0)

它无法工作的原因是因为调用completeRequestReturningItems:completionHandler在最后解除/取消分配ShareViewController,因此永远不会到达完成处理程序,因为它已经杀死了类。

尝试做这样的事情,你会看到:

[imageItemProvider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(NSURL *url, NSError *error)
{
    NSLog(@"Hit here");
    [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}];

答案 4 :(得分:0)

最好的方法是使用加载数据:     [imageItemProvider loadDataRepresentationForTypeIdentifier:(NSString *)kUTTypeImage completionHandler:^(NSData * data,NSError * error){

//用数据做你想做的事

}];