Authored by 山崎友弘

To and modify the "Downloaded image has 0 pixels" error exits bug download a file of the png format

@@ -22,20 +22,17 @@ @@ -22,20 +22,17 @@
22 @autoreleasepool{ 22 @autoreleasepool{
23 // do not decode animated images 23 // do not decode animated images
24 if (image.images) { return image; } 24 if (image.images) { return image; }
25 - 25 +
26 CGImageRef imageRef = image.CGImage; 26 CGImageRef imageRef = image.CGImage;
27 - 27 +
28 CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef); 28 CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
29 BOOL anyAlpha = (alpha == kCGImageAlphaFirst || 29 BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
30 alpha == kCGImageAlphaLast || 30 alpha == kCGImageAlphaLast ||
31 alpha == kCGImageAlphaPremultipliedFirst || 31 alpha == kCGImageAlphaPremultipliedFirst ||
32 alpha == kCGImageAlphaPremultipliedLast); 32 alpha == kCGImageAlphaPremultipliedLast);
33 - 33 +
34 if (anyAlpha) { return image; } 34 if (anyAlpha) { return image; }
35 -  
36 - size_t width = CGImageGetWidth(imageRef);  
37 - size_t height = CGImageGetHeight(imageRef);  
38 - 35 +
39 // current 36 // current
40 CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); 37 CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
41 CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef); 38 CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
@@ -43,19 +40,26 @@ @@ -43,19 +40,26 @@
43 bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed); 40 bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed);
44 if (unsupportedColorSpace) 41 if (unsupportedColorSpace)
45 colorspaceRef = CGColorSpaceCreateDeviceRGB(); 42 colorspaceRef = CGColorSpaceCreateDeviceRGB();
46 -  
47 - CGContextRef context = CGBitmapContextCreate(NULL, width, 43 +
  44 + size_t width = CGImageGetWidth(imageRef);
  45 + size_t height = CGImageGetHeight(imageRef);
  46 + NSUInteger bytesPerPixel = 4;
  47 + NSUInteger bytesPerRow = bytesPerPixel * width;
  48 + NSUInteger bitsPerComponent = 8;
  49 +
  50 + CGContextRef context = CGBitmapContextCreate(NULL,
  51 + width,
48 height, 52 height,
49 - CGImageGetBitsPerComponent(imageRef),  
50 - 0, 53 + bitsPerComponent,
  54 + bytesPerRow,
51 colorspaceRef, 55 colorspaceRef,
52 kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); 56 kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
53 - 57 +
54 // Draw the image into the context and retrieve the new image, which will now have an alpha layer 58 // Draw the image into the context and retrieve the new image, which will now have an alpha layer
55 CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 59 CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
56 CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context); 60 CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
57 UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha scale:image.scale orientation:image.imageOrientation]; 61 UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha scale:image.scale orientation:image.imageOrientation];
58 - 62 +
59 if (unsupportedColorSpace) 63 if (unsupportedColorSpace)
60 CGColorSpaceRelease(colorspaceRef); 64 CGColorSpaceRelease(colorspaceRef);
61 65