didFinishPickingMediaWithInfo return nil photo

2020-01-23 17:07发布

I am working to capture an image that is returned in 4.0 using

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 

    // MediaType can be kUTTypeImage or kUTTypeMovie. If it's a movie then you
    // can get the URL to the actual file itself. This example only looks for images.
    //   
    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    // NSString* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];

    // Try getting the edited image first. If it doesn't exist then you get the
    // original image.
    //
    if (CFStringCompare((CFStringRef) mediaType,  kUTTypeImage, 0) == kCFCompareEqualTo) {               
        UIImage* picture = [info objectForKey:UIImagePickerControllerEditedImage];
        if (!picture)
            picture = [info objectForKey:UIImagePickerControllerOriginalImage];             

        // **picture is always nil
            // info dictionary count = 1


    }

}

What is happening is that the info dictionary always returns with a single entry:

{ UIImagePickerControllerMediaType = "public.image";

which is great, but there is never an image.

I was using a great example from this forum to do this, and I am pretty sure the calls are correct, but never an image.

Thanks in advance!

8条回答
看我几分像从前
2楼-- · 2020-01-23 17:32

use

[[UIImagePickerController alloc] init];

instead of

[[UIImagePickerController alloc] initWithNibName:(NSString *) bundle:(NSBundle *)];
查看更多
贪生不怕死
3楼-- · 2020-01-23 17:33

I know this is many months later, but I struggled with this for hours, and found this same question all over this site and iphonedevsdk.com, but never with a working answer.

To summarize, if the image was picked from the camera roll/photo library it worked fine, but if the image was a new photo take with the camera it never worked. Well, here's how to make it work for both:

You have to dismiss and release the UIImagePickerController before you try to do anything with the info dictionary. To be super-clear:

This DOES NOT work:

- (void)imagePickerController:(UIImagePickerController *)picker 
                      didFinishPickingMediaWithInfo:(NSDictionary *)info {

          // No good with the edited image (if you allowed editing)
  myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
          // AND no good with the original image
  myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
          // AND no doing some other kind of assignment
  UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];

  [picker dismissModalViewControllerAnimated:YES];
  [picker release];
}

In all of those cases the image will be nil.

However, via the magic of releasing the UIImagePickerController first...

This DOES work:

- (void)imagePickerController:(UIImagePickerController *)picker 
                      didFinishPickingMediaWithInfo:(NSDictionary *)info {

  [picker dismissModalViewControllerAnimated:YES];
  [picker release];

          // Edited image works great (if you allowed editing)
  myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
          // AND the original image works great
  myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
          // AND do whatever you want with it, (NSDictionary *)info is fine now
  UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
}

Crazy simple, but that's all there is to it.

查看更多
太酷不给撩
4楼-- · 2020-01-23 17:33

I had the same symptoms, but in my case it turned out I had a category on something in the UIImagePickerController hierarchy that overrode "uuid" or "setUUID"? I guess CoreData needs these methods or it gets very, very confused, and the asset library is all CoreData stuff.

查看更多
该账号已被封号
5楼-- · 2020-01-23 17:38

There's also a known bug with some versions of XCode and Mountain Lion. Check your console for this message:

2013-01-17 21:36:34.946 3sure-ios[5341:1803] Named service 'com.apple.PersistentURLTranslator.Gatekeeper' not found. assetsd is down or misconfigured. Things will not work the way you expect them to.

It probably won't work if that message appears. Check your app at the actual iOS device.

Nevetheless, restarting both simulator and XCode seems to fix the issue.

查看更多
再贱就再见
6楼-- · 2020-01-23 17:40

whilst Matthew Frederick's answer is most popular and has long been the appropriate response, as of iOS 5.0, apple made available dismissViewControllerAnimated:completion:, to replace the now deprecated (as of iOS 6.0) dismissViewControllerAnimated:.

performing the image info dictionary retrieval in the completion block should hopefully make more sense to all.

to take his example from above, it would now look like:

- (void)    imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    [picker dismissViewControllerAnimated:YES completion:^{
          // Edited image works great (if you allowed editing)
        myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
          // AND the original image works great
        myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
          // AND do whatever you want with it, (NSDictionary *)info is fine now
        UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
    }];
}
查看更多
家丑人穷心不美
7楼-- · 2020-01-23 17:45

I've tried all above, but no luck on iPad 6.0/6.1 simulator, however i've found that info contains "UIImagePickerControllerReferenceURL" key and here is my code:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  [picker dismissViewControllerAnimated:YES completion:NULL];

  UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
  if(NULL == image){
      [MyImageLoader loadImageFromAssertByUrl:[info objectForKey:@"UIImagePickerControllerReferenceURL"]
                                   completion:^(UIImage* img){
                                        //img not null here
                                   }];
  }else{
      //image not null here
  }
}

and code for loadImageFromAssertByUrl is:

+(void) loadImageFromAssertByUrl:(NSURL *)url completion:(void (^)(UIImage*)) completion{
  ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
  [assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
      ALAssetRepresentation *rep = [asset defaultRepresentation];
      Byte *buffer = (Byte*)malloc(rep.size);
      NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
      NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
      UIImage* img = [UIImage imageWithData:data];
      completion(img);
  } failureBlock:^(NSError *err) {
      NSLog(@"Error: %@",[err localizedDescription]);
  }];
}
查看更多
登录 后发表回答