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

时间:2022-04-24 21:23:19

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

问题是我无法判断是将其存储为PNG还是JPEG。我不能总是将它存储为PNG,因为对于较大的照片,它是无比缓慢的(更不用说我必须分别处理存储图像方向)。我不能总是将它存储为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? Maybe some method of just checking if the image has any transparency or something?

有没有办法知道用户是否选择了PNG?也许某种方法只是检查图像是否有任何透明度或什么?

Thanks.

谢谢。

2 个解决方案

#1


2  

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所示

#2


6  

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!

如果有人有更好的方法,我很乐意听到他们。谢谢!

#1


2  

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所示

#2


6  

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!

如果有人有更好的方法,我很乐意听到他们。谢谢!