From 32ca18b5232a471936508ff3068d16b65ee42dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=B1=E5=B4=8E=E5=8F=8B=E5=BC=98?= Date: Tue, 15 Mar 2016 18:07:57 +0900 Subject: [PATCH] To and modify the "Downloaded image has 0 pixels" error exits bug download a file of the png format --- SDWebImage/SDWebImageDecoder.m | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/SDWebImage/SDWebImageDecoder.m b/SDWebImage/SDWebImageDecoder.m index a7c02468..7a140dcf 100644 --- a/SDWebImage/SDWebImageDecoder.m +++ b/SDWebImage/SDWebImageDecoder.m @@ -22,20 +22,17 @@ @autoreleasepool{ // do not decode animated images if (image.images) { return image; } - + CGImageRef imageRef = image.CGImage; - + CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef); BOOL anyAlpha = (alpha == kCGImageAlphaFirst || alpha == kCGImageAlphaLast || alpha == kCGImageAlphaPremultipliedFirst || alpha == kCGImageAlphaPremultipliedLast); - + if (anyAlpha) { return image; } - - size_t width = CGImageGetWidth(imageRef); - size_t height = CGImageGetHeight(imageRef); - + // current CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef); @@ -43,19 +40,26 @@ bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed); if (unsupportedColorSpace) colorspaceRef = CGColorSpaceCreateDeviceRGB(); - - CGContextRef context = CGBitmapContextCreate(NULL, width, + + size_t width = CGImageGetWidth(imageRef); + size_t height = CGImageGetHeight(imageRef); + NSUInteger bytesPerPixel = 4; + NSUInteger bytesPerRow = bytesPerPixel * width; + NSUInteger bitsPerComponent = 8; + + CGContextRef context = CGBitmapContextCreate(NULL, + width, height, - CGImageGetBitsPerComponent(imageRef), - 0, + bitsPerComponent, + bytesPerRow, colorspaceRef, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); - + // Draw the image into the context and retrieve the new image, which will now have an alpha layer CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context); UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha scale:image.scale orientation:image.imageOrientation]; - + if (unsupportedColorSpace) CGColorSpaceRelease(colorspaceRef);