Sdwebimage: why not decode image with alpha ?

Created on 23 Aug 2017  路  1Comment  路  Source: SDWebImage/SDWebImage

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

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohacs picture mohacs  路  5Comments

MagLiC picture MagLiC  路  3Comments

Binusz picture Binusz  路  4Comments

diogot picture diogot  路  4Comments

ku8ar picture ku8ar  路  4Comments