来自UIImagePickerController的UIImage的NSURL返回(空) [英] NSURL of UIImage from UIImagePickerController returns (null)

查看:284
本文介绍了来自UIImagePickerController的UIImage的NSURL返回(空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editInfo {
userURL = [editInfo objectForKey:UIImagePickerControllerMediaURL];
userImage = image;
userImageView.image=userImage;
[self dismissViewControllerAnimated:YES completion:nil];}

然后我使用NSURL userURL,并将其放在UIActivityViewController中以用于上传图片。但是,此操作永远无效,并且在尝试上传(空)时始终会失败。但是,当我使用xcode项目和以下代码中包含的预设图像时,它始终可以正常工作并正确上传:

I then take NSURL userURL, and put it in an UIActivityViewController to use to upload the image. However this never works, and always fails as it's trying to upload (null). However, when I use a preset image included in the xcode project and the following code, it always works and uploads correctly:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"kitten.jpg" withExtension:nil];

如果有帮助,我正在使用 https://github.com/goosoftware/GSDropboxActivity

If it helps, I'm using https://github.com/goosoftware/GSDropboxActivity

当我使用UIImagePickerControllerReferenceURL而不是UIImagePickerControllerMediaURL时,我得到了以下错误:
[警告] DropboxSDK:文件不存在(/asset.JPG)
无法上载资产-library://asset/asset.JPG?id = EECAF4D0-A5ED-40E7- 8E6F-3A586C0AB06E& ext = JPG

When I use UIImagePickerControllerReferenceURL instead of UIImagePickerControllerMediaURL, I get the following error: [WARNING] DropboxSDK: File does not exist (/asset.JPG) Failed to upload assets-library://asset/asset.JPG?id=EECAF4D0-A5ED-40E7-8E6F-3A586C0AB06E&ext=JPG

推荐答案

从理论上讲, UIImagePickerControllerMediaURL 是仅针对电影而非图片进行填充。如果使用 UIImagePickerControllerReferenceURL ,则给出的URL不是文件系统的URL,而是资产库。要从中获取文件,您需要使用资产库。使用类似以下的内容:

In theory, UIImagePickerControllerMediaURL is only populated for a movie, not an image. If you use the UIImagePickerControllerReferenceURL then the URL that you're given is not a URL for the file system, but rather the assets library. To get the file from that you'll need to use the asset library. Use something like the following :

typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);    

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){

    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];

    if (iref){

        UIImage *myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];

         // Do whatever you now want with your UIImage
     }      
};      

ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror){                   
    //failed to get image.
};                          

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:[filePath objectAtIndex:0] resultBlock:resultblock failureBlock:failureblock];

这将处理将处理您的请求的块,但是您仍然需要实际请求资源从资产库。为此,请尝试:

This takes care of the blocks that will handle your request, but you still need to actually request the resource from the assets library. For that, try this :

ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
NSURL myAssetUrl = [NSURL URLWithString:[editInfo objectForKey:UIImagePickerControllerMediaURL]];
[assetslibrary assetForURL:myAssetUrl resultBlock:resultblock failureBlock:failureblock];

从理论上讲,您应该得到的是:)
这是由 Ramshad 提供的,对此的进一步讨论可以在此处

And in theory, you should get what you're after :) The code for this was given by Ramshad and further discussion of it can be found here

希望这对您有所帮助你在追。抱歉,为时已晚:(

Hopefully this will help you with what you're after. Sorry if it's a bit too late :(

编辑

您没有使用ARC,在此示例中,您需要整理内存,因为我根本没有包含任何内存管理。

Note that if you're not using ARC, you'll need to tidy up the memory in this example as I haven't included any memory management at all.

这篇关于来自UIImagePickerController的UIImage的NSURL返回(空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆