Merge pull request #277 from Reflejo/upstream
Fixed CGBitmapContextCreate warnings for invalid alpha properties
This commit is contained in:
commit
0ee103263c
|
@ -13,13 +13,44 @@
|
||||||
@implementation UIImage (ForceDecode)
|
@implementation UIImage (ForceDecode)
|
||||||
|
|
||||||
+ (UIImage *)decodedImageWithImage:(UIImage *)image
|
+ (UIImage *)decodedImageWithImage:(UIImage *)image
|
||||||
{
|
{
|
||||||
CGImageRef imageRef = image.CGImage;
|
CGImageRef imageRef = image.CGImage;
|
||||||
CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
|
CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
|
||||||
CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};
|
CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};
|
||||||
|
|
||||||
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||||
CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpace, CGImageGetBitmapInfo(imageRef));
|
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
|
||||||
|
|
||||||
|
// CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB.
|
||||||
|
// https://developer.apple.com/library/mac/#qa/qa1037/_index.html
|
||||||
|
if ((bitmapInfo & kCGBitmapAlphaInfoMask) == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) > 1)
|
||||||
|
{
|
||||||
|
// Unset the old alpha info.
|
||||||
|
bitmapInfo &= ~kCGBitmapAlphaInfoMask;
|
||||||
|
|
||||||
|
// Set noneSkipFirst.
|
||||||
|
bitmapInfo |= kCGImageAlphaNoneSkipFirst;
|
||||||
|
}
|
||||||
|
// Some PNGs tell us they have alpha but only 3 components. Odd.
|
||||||
|
else if (((bitmapInfo & kCGBitmapAlphaInfoMask) & (kCGImageAlphaFirst | kCGImageAlphaLast)) != 0
|
||||||
|
&& CGColorSpaceGetNumberOfComponents(colorSpace) == 3)
|
||||||
|
{
|
||||||
|
// Unset the old alpha info.
|
||||||
|
bitmapInfo &= ~kCGBitmapAlphaInfoMask;
|
||||||
|
|
||||||
|
// Set noneSkipFirst.
|
||||||
|
bitmapInfo |= kCGImageAlphaPremultipliedFirst;
|
||||||
|
}
|
||||||
|
|
||||||
|
// It calculates the bytes-per-row based on the bitsPerComponent and width arguments.
|
||||||
|
CGContextRef context = CGBitmapContextCreate(NULL,
|
||||||
|
imageSize.width,
|
||||||
|
imageSize.height,
|
||||||
|
CGImageGetBitsPerComponent(imageRef),
|
||||||
|
0,
|
||||||
|
colorSpace,
|
||||||
|
bitmapInfo);
|
||||||
|
CGColorSpaceRelease(colorSpace);
|
||||||
|
|
||||||
// If failed, return undecompressed image
|
// If failed, return undecompressed image
|
||||||
if (!context) return image;
|
if (!context) return image;
|
||||||
|
|
Loading…
Reference in New Issue