Skip to content

Commit

Permalink
Only store an image with unknown format as PNG if it has an alpha cha…
Browse files Browse the repository at this point in the history
…nnel.
  • Loading branch information
archfear committed Apr 3, 2015
1 parent 7e50d78 commit 38a6edb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions SDWebImage/SDImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate image
// The first eight bytes of a PNG file always contain the following (decimal) values:
// 137 80 78 71 13 10 26 10

// We assume the image is PNG, in case the imageData is nil (i.e. if trying to save a UIImage directly),
// we will consider it PNG to avoid loosing the transparency
BOOL imageIsPng = YES;
// If the imageData is nil (i.e. if trying to save a UIImage directly or the image was transformed on download)
// and the image has an alpha channel, we will consider it PNG to avoid losing the transparency
int alphaInfo = CGImageGetAlphaInfo(image.CGImage);
BOOL hasAlpha = !(alphaInfo == kCGImageAlphaNone ||
alphaInfo == kCGImageAlphaNoneSkipFirst ||
alphaInfo == kCGImageAlphaNoneSkipLast);
BOOL imageIsPng = hasAlpha;

// But if we have an image data, we will look at the preffix
if ([imageData length] >= [kPNGSignatureData length]) {
Expand Down
2 changes: 1 addition & 1 deletion SDWebImage/SDWebImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ - (void)diskImageExistsForURL:(NSURL *)url

if (transformedImage && finished) {
BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage];
[self.imageCache storeImage:transformedImage recalculateFromImage:imageWasTransformed imageData:imageWasTransformed ? nil : data forKey:key toDisk:cacheOnDisk];
[self.imageCache storeImage:transformedImage recalculateFromImage:imageWasTransformed imageData:(imageWasTransformed ? nil : data) forKey:key toDisk:cacheOnDisk];
}

dispatch_main_sync_safe(^{
Expand Down

0 comments on commit 38a6edb

Please sign in to comment.