CGImageRef imageRef = image.CGImage;
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
// do not decode images with alpha
if (anyAlpha) {
return NO;
}
You are misunderstanding by that decode word. This should be named decompress or decode before rendering. This shouldDecodeImage method is used for that UIImage+ForceDecode category.
UIImage is just an wrapper to CGImage. When you use that +[UIImage imageWithData:] to create an UIImage instance, the image data will not decode to image bitmap immediately(You won't see any memory increate when you call this method). Instead, the underneath CGImage instance is just created with Image/IO which keep an reference to the image data. It will be decoded the first time UIImageView rendering(You can learn this behavior from other references and I will not talk detail implementation here).
But sometimes we need to decompressed first before UIImageView rendering. This is an feature that may help you increase frame rate(because the rendering process do not need extra decoding CPU usage) but this also means you need extra memory before you set UIImage to UIImageView. So we have that UIImage+ForceDecode category.
For historic reason, the ForceDecode process happend for any images. But there seems some bugs related to alpha PNG. So then we drop the process for alpha image and only decompress non-alpha images. You can also set shouldDecompressImages to NO to disable this feature totally.
Most helpful comment
You are misunderstanding by that
decodeword. This should be nameddecompressordecode before rendering. ThisshouldDecodeImagemethod is used for thatUIImage+ForceDecodecategory.UIImageis just an wrapper toCGImage. When you use that+[UIImage imageWithData:]to create an UIImage instance, the image data will not decode to image bitmap immediately(You won't see any memory increate when you call this method). Instead, the underneathCGImageinstance is just created with Image/IO which keep an reference to the image data. It will be decoded the first time UIImageView rendering(You can learn this behavior from other references and I will not talk detail implementation here).But sometimes we need to decompressed first before UIImageView rendering. This is an feature that may help you increase frame rate(because the rendering process do not need extra decoding CPU usage) but this also means you need extra memory before you set UIImage to UIImageView. So we have that
UIImage+ForceDecodecategory.For historic reason, the ForceDecode process happend for any images. But there seems some bugs related to alpha PNG. So then we drop the process for alpha image and only decompress non-alpha images. You can also set
shouldDecompressImagesto NO to disable this feature totally.