fix unsupported parameter combination issues
when CGBitmapContextCreate is called
This commit is contained in:
parent
dcde40fc01
commit
4cfb12c01c
|
@ -13,60 +13,38 @@
|
||||||
@implementation UIImage (ForceDecode)
|
@implementation UIImage (ForceDecode)
|
||||||
|
|
||||||
+ (UIImage *)decodedImageWithImage:(UIImage *)image {
|
+ (UIImage *)decodedImageWithImage:(UIImage *)image {
|
||||||
if (image.images) {
|
// do not decode animated images
|
||||||
// Do not decode animated images
|
if (image.images) { return image; }
|
||||||
return image;
|
|
||||||
}
|
|
||||||
|
|
||||||
CGImageRef imageRef = image.CGImage;
|
CGImageRef imageRef = image.CGImage;
|
||||||
CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
|
|
||||||
CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};
|
|
||||||
|
|
||||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
|
||||||
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
|
BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
|
||||||
|
alpha == kCGImageAlphaLast ||
|
||||||
|
alpha == kCGImageAlphaPremultipliedFirst ||
|
||||||
|
alpha == kCGImageAlphaPremultipliedLast);
|
||||||
|
|
||||||
int infoMask = (bitmapInfo & kCGBitmapAlphaInfoMask);
|
if (anyAlpha) { return image; }
|
||||||
BOOL anyNonAlpha = (infoMask == kCGImageAlphaNone ||
|
|
||||||
infoMask == kCGImageAlphaNoneSkipFirst ||
|
|
||||||
infoMask == kCGImageAlphaNoneSkipLast);
|
|
||||||
|
|
||||||
// CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB.
|
size_t width = CGImageGetWidth(imageRef);
|
||||||
// https://developer.apple.com/library/mac/#qa/qa1037/_index.html
|
size_t height = CGImageGetHeight(imageRef);
|
||||||
if (infoMask == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) > 1) {
|
|
||||||
// Unset the old alpha info.
|
|
||||||
bitmapInfo &= ~kCGBitmapAlphaInfoMask;
|
|
||||||
|
|
||||||
// Set noneSkipFirst.
|
CGContextRef context = CGBitmapContextCreate(NULL, width,
|
||||||
bitmapInfo |= kCGImageAlphaNoneSkipFirst;
|
height,
|
||||||
}
|
CGImageGetBitsPerComponent(imageRef),
|
||||||
// Some PNGs tell us they have alpha but only 3 components. Odd.
|
0,
|
||||||
else if (!anyNonAlpha && CGColorSpaceGetNumberOfComponents(colorSpace) == 3) {
|
CGImageGetColorSpace(imageRef),
|
||||||
// Unset the old alpha info.
|
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
|
||||||
bitmapInfo &= ~kCGBitmapAlphaInfoMask;
|
|
||||||
bitmapInfo |= kCGImageAlphaPremultipliedFirst;
|
|
||||||
}
|
|
||||||
|
|
||||||
// It calculates the bytes-per-row based on the bitsPerComponent and width arguments.
|
// Draw the image into the context and retrieve the new image, which will now have an alpha layer
|
||||||
CGContextRef context = CGBitmapContextCreate(NULL,
|
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
|
||||||
imageSize.width,
|
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
|
||||||
imageSize.height,
|
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];
|
||||||
CGImageGetBitsPerComponent(imageRef),
|
|
||||||
0,
|
|
||||||
colorSpace,
|
|
||||||
bitmapInfo);
|
|
||||||
CGColorSpaceRelease(colorSpace);
|
|
||||||
|
|
||||||
// If failed, return undecompressed image
|
|
||||||
if (!context) return image;
|
|
||||||
|
|
||||||
CGContextDrawImage(context, imageRect, imageRef);
|
|
||||||
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);
|
|
||||||
|
|
||||||
CGContextRelease(context);
|
CGContextRelease(context);
|
||||||
|
CGImageRelease(imageRefWithAlpha);
|
||||||
|
|
||||||
UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation];
|
return imageWithAlpha;
|
||||||
CGImageRelease(decompressedImageRef);
|
|
||||||
return decompressedImage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Reference in New Issue