来自UIImagePickerController的UIImage的NSURL返回(null)

时间:2012-12-08 01:47:06

标签: objective-c uiimage uiimagepickercontroller dropbox 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中以用于上传图像。然而,这永远不会起作用,并且总是因为它试图上传(null)而失败。但是,当我使用xcode项目中包含的预设图像和以下代码时,它总能正常工作并正确上传:

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

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

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

1 个答案:

答案 0 :(得分:0)

理论上,UIImagePickerControllerMediaURL仅适用于电影,而不是图像。如果您使用UIImagePickerControllerReferenceURL,那么您提供的URL不是文件系统的URL,而是资产库。要从中获取文件,您需要使用资产库。使用以下内容:

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

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

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

从理论上讲,你应该得到你想要的东西:) 这个代码由Ramshad给出,可以在here

找到对它的进一步讨论

希望这可以帮助你完成你所追求的目标。对不起,如果有点太晚了:(

修改

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