Here is a question for the very experienced iOS developer.
The Core Graphics function CGImageMaskCreate creates a CG image mask from a grayscale image. I am using a JPG exported as grayscale. I double checked: When opening in Photoshop, going to Image > Mode > it says "Grayscale".
The following method is part of a UIImage category. It takes a mask UIImage (just a grayscale JPG) and its goal is to mask itself (a UIImage which is a JPG photo) so the resulting image has transparency.
It works on all 4 inch devices and on iPad 4. But the output of CGImageMaskCreate is dead on any 3.5 inch device with iOS 6 or iOS 7. That is, the resulting image is just 100% transparent. Same in iPhone simulator 3.5 inch (Xcode 5). I have no clue why. Some people say this is a iOS 7 problem, but it works on iPhone 5 and 5S with iOS 7. I also tried PNG-8 grayscale with same result.
Is there a known TN from Apple or a workaround to this bug?
- (UIImage*)maskedImageWithMask:(UIImage *)maskImage {
CGImageRef imageRef = self.CGImage;
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, NO);
CGImageRef maskedImageRef = CGImageCreateWithMask(imageRef, mask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef scale:self.scale orientation:self.imageOrientation];
CGImageRelease(mask);
CGImageRelease(maskedImageRef);
return maskedImage;
}
Edit:
On 3.5-inch iPhone simulator and devices CGImageIsMask(mask) returns NO. On 4-inch simulator and devices it returns YES. So CGImageMaskCreate definitely creates no mask sometimes.