问题
I'm using an iPhone 7 running iOS 10 (doesn't appear to matter which version of iOS 10, but it has to be an iPhone 7 device...even an iPhone 6s with the same version of iOS 10 doesn't have this issue). Unfortunately, I don't have the ability to try an iPhone 7 running iOS 9 or below.
In my app, I'm using this method to mask an image with another image:
- (UIImage *)imageWithMask:(UIImage *)maskImage
{
UIImage *returnImage = nil;
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef),
NULL,
false);
CGImageRef maskedImageRef = CGImageCreateWithMask([self CGImage], mask);
returnImage = [UIImage imageWithCGImage:maskedImageRef];
CGImageRelease(mask);
CGImageRelease(maskedImageRef);
return returnImage;
}
Here's the mask image I'm passing (black and white):
If I build my app with Xcode 7.3.1, my method masks the image fine:
However, if I run the app in Xcode 8 (even the latest 8.2), my method causes the resulting image to look transparent (the white background comes through):
I don't understand what is going on and why the different versions of Xcode would be causing this (and why it's only happening on iPhone 7). Maybe the different versions of Xcode are pulling in different versions of CoreGraphics or something? Maybe something about CoreGraphics with iPhone 7 is different than it is with iPhone 6s (possibly because of iPhone 7's screen resolution or processing capabilities or something)?
I'm having trouble figuring out what to try / look into next, now that I know it's an Xcode issue.
回答1:
So far, the only answer I have is to skip CGImageMaskCreate
and CGImageCreateWithMask
altogether and utilize UIView
's maskView
property with an alpha mask icon.
In other words, now instead of masking the UIImage
and drawing it in a certain CGRect
frame on the screen, now I'm doing this:
imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = imageToMask;
imageView.maskView = [[UIImageView alloc] initWithImage:alphaMaskIcon];
[view addSubview:imageView];
One important note about this method is that the maskView
uses the alpha channels of the pixels in the masking icon to determine what to mask or let through, rather than using the 0.0-1.0 grayscale value of the pixels in the black/white icon. So, using my bubble icon as an example, the black should still be solid black because I want that part of the image to come through, but the white around it now has to be transparent because I want that part of the image to be masked.
来源:https://stackoverflow.com/questions/41151557/xcode-8-coregraphics-issue-with-iphone-7-running-ios-10