didFinishPickingMediaWithInfo返回零照片

Rob*_*ner 30 iphone camera uiimagepickercontroller uiimage

我正在努力捕获使用4.0返回的图像

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


    }

}
Run Code Online (Sandbox Code Playgroud)

发生的事情是信息字典总是返回一个条目:

{UIImagePickerControllerMediaType ="public.image";

这很棒,但从来没有形象.

我在这个论坛上使用了一个很好的例子来做到这一点,我很确定这些调用是正确的,但从来都不是图像.

提前致谢!

Mat*_*ick 54

我知道这是几个月之后,但是我在这个问题上挣扎了好几个小时,并且在整个网站和iphonedevsdk.com上发现了同样的问题,但从来没有找到合适的答案.

总而言之,如果从相机胶卷/照片库中拾取图像它可以正常工作,但如果图像是用相机拍摄的新照片则无法正常工作.那么,这是如何让它适用于两者:

尝试对信息字典执行任何操作之前,必须先解除并释放UIImagePickerController .要非常清楚:

这不起作用:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

在所有这些情况下,图像将为零.

然而,通过首先释放UIImagePickerController的魔力......

这个工作:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

疯狂简单,但这就是它的全部.

  • 你是认真的!?!这就是我的问题!我的天啊.*_dies_* (12认同)

joh*_*doe 17

虽然马修弗雷德里克的答案是最受欢迎的,并且长期以来一直是适当的回应,因为苹果提供的iOS 5.0 dismissViewControllerAnimated:completion:,以取代现在已弃用的(从iOS 6.0开始)dismissViewControllerAnimated:.

在完成块中执行图像信息字典检索应该有希望对所有人更有意义.

从上面看他的例子,它现在看起来像:

- (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];
    }];
}
Run Code Online (Sandbox Code Playgroud)

  • 对于我来说,在iPad模拟器(iOS 6.1)中运行iPhone应用程序仍然会产生零结果. (2认同)

Ale*_*rov 5

我已经尝试了以上所有,但在iPad 6.0/6.1模拟器上没有运气,但是我发现信息包含"UIImagePickerControllerReferenceURL"键,这里是我的代码:

- (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
  }
}
Run Code Online (Sandbox Code Playgroud)

和loadImageFromAssertByUrl的代码是:

+(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]);
  }];
}
Run Code Online (Sandbox Code Playgroud)