Authored by Konstantinos K

Merge pull request #1121 from archfear/transparancy_fix

Fix for transparency being lost in transformed images.
@@ -198,9 +198,13 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -198,9 +198,13 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
198 // The first eight bytes of a PNG file always contain the following (decimal) values: 198 // The first eight bytes of a PNG file always contain the following (decimal) values:
199 // 137 80 78 71 13 10 26 10 199 // 137 80 78 71 13 10 26 10
200 200
201 - // We assume the image is PNG, in case the imageData is nil (i.e. if trying to save a UIImage directly),  
202 - // we will consider it PNG to avoid loosing the transparency  
203 - BOOL imageIsPng = YES; 201 + // If the imageData is nil (i.e. if trying to save a UIImage directly or the image was transformed on download)
  202 + // and the image has an alpha channel, we will consider it PNG to avoid losing the transparency
  203 + int alphaInfo = CGImageGetAlphaInfo(image.CGImage);
  204 + BOOL hasAlpha = !(alphaInfo == kCGImageAlphaNone ||
  205 + alphaInfo == kCGImageAlphaNoneSkipFirst ||
  206 + alphaInfo == kCGImageAlphaNoneSkipLast);
  207 + BOOL imageIsPng = hasAlpha;
204 208
205 // But if we have an image data, we will look at the preffix 209 // But if we have an image data, we will look at the preffix
206 if ([imageData length] >= [kPNGSignatureData length]) { 210 if ([imageData length] >= [kPNGSignatureData length]) {
@@ -216,7 +216,7 @@ @@ -216,7 +216,7 @@
216 216
217 if (transformedImage && finished) { 217 if (transformedImage && finished) {
218 BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage]; 218 BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage];
219 - [self.imageCache storeImage:transformedImage recalculateFromImage:imageWasTransformed imageData:data forKey:key toDisk:cacheOnDisk]; 219 + [self.imageCache storeImage:transformedImage recalculateFromImage:imageWasTransformed imageData:(imageWasTransformed ? nil : data) forKey:key toDisk:cacheOnDisk];
220 } 220 }
221 221
222 dispatch_main_sync_safe(^{ 222 dispatch_main_sync_safe(^{