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

This commit is contained in:
山崎友弘 2016-03-15 18:07:57 +09:00
parent 469d8bbfd8
commit 32ca18b523
1 changed files with 17 additions and 13 deletions

View File

@ -22,20 +22,17 @@
@autoreleasepool{ @autoreleasepool{
// do not decode animated images // do not decode animated images
if (image.images) { return image; } if (image.images) { return image; }
CGImageRef imageRef = image.CGImage; CGImageRef imageRef = image.CGImage;
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef); CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
BOOL anyAlpha = (alpha == kCGImageAlphaFirst || BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast || alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst || alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast); alpha == kCGImageAlphaPremultipliedLast);
if (anyAlpha) { return image; } if (anyAlpha) { return image; }
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
// current // current
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef); CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
@ -43,19 +40,26 @@
bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed); bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed);
if (unsupportedColorSpace) if (unsupportedColorSpace)
colorspaceRef = CGColorSpaceCreateDeviceRGB(); 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, height,
CGImageGetBitsPerComponent(imageRef), bitsPerComponent,
0, bytesPerRow,
colorspaceRef, colorspaceRef,
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
// Draw the image into the context and retrieve the new image, which will now have an alpha layer // 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); CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context); CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha scale:image.scale orientation:image.imageOrientation]; UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha scale:image.scale orientation:image.imageOrientation];
if (unsupportedColorSpace) if (unsupportedColorSpace)
CGColorSpaceRelease(colorspaceRef); CGColorSpaceRelease(colorspaceRef);