NSURL of UIImage from UIImagePickerController returns (null)

别说谁变了你拦得住时间么 提交于 2019-12-11 10:35:50

问题


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

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

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

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


回答1:


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

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 :(

Edit

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.



来源:https://stackoverflow.com/questions/13773635/nsurl-of-uiimage-from-uiimagepickercontroller-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!