didFinishPickingMediaWithInfo返回nil照片 [英] didFinishPickingMediaWithInfo return nil photo

查看:1137
本文介绍了didFinishPickingMediaWithInfo返回nil照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

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

{ 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.

提前感谢!

推荐答案

后来,但我努力了这个几个小时,在这个网站和iphonedevsdk.com发现了这个相同的问题,但从来没有一个工作的答案。

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:

你必须关闭并释放UIImagePickerController 之前你尝试做任何事情的信息字典。非常清楚:

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

这不能正常工作

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

在所有这些情况下,图像将为nil。

In all of those cases the image will be nil.

但是,通过发布第一个UIImagePickerController 的魔法...

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.

这篇关于didFinishPickingMediaWithInfo返回nil照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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