简体   繁体   English

使用UIImagePickerController获取图像 - 如何知道是否保存PNG或JPEG?

[英]Using UIImagePickerController to get image — how to know whether to save PNG or JPEG?

I've got a UIImagePickerController letting the user pick an image out of the image library, and am getting its results via the didFinishPickingMediaWithInfo method. 我有一个UIImagePickerController让用户从图像库中选择一个图像,并通过didFinishPickingMediaWithInfo方法获得结果。

I need to be able to save the resulting image to disk (in the app's documents folder), and reload it later. 我需要能够将生成的图像保存到磁盘(在应用程序的文档文件夹中),并在以后重新加载。

The issue is that I can't tell whether to store it as a PNG or JPEG. 问题是我无法判断是将其存储为PNG还是JPEG。 I can't just always store it as a PNG, because for larger photos it's interminably slow (not to mention then I have to deal with storing the image orientation separately). 我不能总是将它存储为PNG,因为对于较大的照片,它的速度很慢(更不用说我必须单独处理存储图像方向)。 I can't just always store it as a JPEG, because in some cases the images have transparency, which will get lost if I do that. 我不能总是将它存储为JPEG,因为在某些情况下,图像具有透明度,如果我这样做会丢失。

I've examined the UIImagePickerControllerMediaType key in the info dictionary returned by the image picker, and regardless of whether I've selected a PNG or JPEG, what gets returned is "image.public" . 我已经检查了图像选择器返回的信息字典中的UIImagePickerControllerMediaType键,无论我选择了PNG还是JPEG,返回的是"image.public"

So... 所以...

Is there some way to know whether the user has chosen a PNG? 有没有办法知道用户是否选择了PNG? Maybe some method of just checking if the image has any transparency or something? 也许只是检查图像是否有任何透明度的某种方法?

Thanks. 谢谢。

OK, so I figured it out. 好的,所以我想通了。 This may not work for every scenario, but it's sufficient for me: 这可能不适用于所有情况,但对我来说已经足够了:

    CGImageAlphaInfo imgAlpha = CGImageGetAlphaInfo(theImage.CGImage);

    // Is this an image with transparency (i.e. do we need to save as PNG?)
    if ((imgAlpha == kCGImageAlphaNone) || (imgAlpha == kCGImageAlphaNoneSkipFirst) || (imgAlpha == kCGImageAlphaNoneSkipLast)) {
         // save as a JPEG
    } else {
         // save as a PNG
    }

...of course you need to remember which type of image you saved, give it the appropriate file extension, and load the right one back in... but basically this takes care of it. ...当然你需要记住你保存了哪种类型的图像,给它适当的文件扩展名,并加载正确的文件...但基本上这样可以解决它。 Images with transparency will be saved as PNGs, everything else as JPEGs. 具有透明度的图像将保存为PNG,其他所有内容均保存为JPEG。

If anyone has any better methods, I'd love to hear them. 如果有人有更好的方法,我很乐意听到。 Thanks! 谢谢!

in the dictionary the key UIImagePickerControllerReferenceURL will give you informations about the type 在字典中,键UIImagePickerControllerReferenceURL将为您提供有关该类型的信息

(gdb) po info (gdb)po信息

{
    UIImagePickerControllerMediaType = "public.image";
    UIImagePickerControllerOriginalImage = "<UIImage: 0x5cfd00>";
    UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=EFC44C3C-9C82-4669-924B-A2B9DE6F1F45&ext=JPG";
}

In this case it is a jpg as shown by ext=JPG 在这种情况下,它是一个jpg,如ext = JPG所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM