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

情到浓时终转凉″ 提交于 2019-12-01 04:23:34

in the dictionary the key UIImagePickerControllerReferenceURL will give you informations about the type

(gdb) po info

{
    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

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.

If anyone has any better methods, I'd love to hear them. Thanks!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!