Sdwebimage: What does these codes marked as "Workaround for iOS anamorphic image" used for

Created on 29 Feb 2016  路  6Comments  路  Source: SDWebImage/SDWebImage

#ifdef TARGET_OS_IPHONE
// Workaround for iOS anamorphic image
if (partialImageRef) {
    const size_t partialHeight = CGImageGetHeight(partialImageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8, width * 4, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(colorSpace);
    if (bmContext) {
        CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = width, .size.height = partialHeight}, partialImageRef);
        CGImageRelease(partialImageRef);
        partialImageRef = CGBitmapContextCreateImage(bmContext);
        CGContextRelease(bmContext);
    }
    else {
        CGImageRelease(partialImageRef);
        partialImageRef = nil;
    }
}
#endif

what does these codes used for while SD has decodedImageWithImage method already

bug

Most helpful comment

Fix by #2303

All 6 comments

Not sure. This code is from 2012. Maybe @rs can answer to this.

Was introduced in https://github.com/rs/SDWebImage/commit/ba71333e17e18ecdef26574963150e3bd3591fca from PR #114

Was about handling of progressive download. Maybe it no longer relevant.

This code...Seems does not make any sence. I also found the original commit is ba71333

If the image is actually partial, which means the height of progressive image is smaller than the full image, we should draw it on a full height bitmap and keep the left space transparent. However, Image/IO already process this, the CGImageSourceCreateImageAtIndex will always contains transparent space for unfinished progressive image. So no need to draw again.

Only WebP coder need this because libwebp always return the partial height image, so we must draw it on a full height bitmap context.

I'd suggest to remove this code in SDWebImageImageIOCoder and use the common SDWebImageAvoidDecodeImage logic to process in 5.x.

OK....I found the original blog about this strange code. See https://cocoaintheshell.whine.fr/2011/05/progressive-images-download-imageio/

It seems that on the earily version of Image/IO, the behavior does not match the current behavior. If the image is not PNG, it will return the actual partial height of total images(like what I talk above). So if you directlly use that image(which height is smaller than the actual height), you will see the stretch version of partial images depends on your imageView's content mode.

However, In my test, iOS 8+ above Image/IO always return the full height images, with the transparent (Or white if you do not support alpha channel) space filling the unfinished height. It works well. See my test about this partialImageRef JPEG image during progressive decoding (lldb debug print):

2018-04-25 1 32 39

The original full image is https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage001.jpg . You can see the both partial image or full image height is the same 766.

So I think, maybe now we can remove that hack code. Because duplicated force decode may impact performance, and may cause the image distortion. We should avoid force decode same image multiple times.

@devshen @bpoplauschi I create the PR about this, see #2303

Fix by #2303

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MagLiC picture MagLiC  路  3Comments

Binusz picture Binusz  路  4Comments

ericeddy picture ericeddy  路  6Comments

paul-lavoine picture paul-lavoine  路  4Comments

mohacs picture mohacs  路  5Comments