1

我有没有阿尔法通道的蒙版图像,纯黑色和白色,中心有一个黑色多边形,周围是白色。

我有要屏蔽的 png 图像。png 有一个 alpha 通道。使用下面的方法:在模拟器上可以,但设备上的背景是黑色的……有什么办法解决这个问题吗?

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

CGImageRef maskRef = maskImage.CGImage; 

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);

//CGImageRelease(mask);
//CGImageRelease(maskRef);
return [UIImage imageWithCGImage:masked];

}

4

2 回答 2

2

已解决的问题:解决方案是:要屏蔽的图像必须使用 Alpha 通道创建。Alpha 通道可能不是从代码创建的。

于 2010-07-22T16:11:32.510 回答
0

与以下代码相同

- (UIImage*)maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef sourceImage = [image CGImage];
CGImageRef imageWithAlpha = sourceImage;
//add alpha channel for images that don't have one (ie GIF, JPEG, etc...)
//this however has a computational cost
if (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) { 
    imageWithAlpha = CopyImageAndAddAlphaChannel(sourceImage);
}

CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
//const float colorMasking[6] = { 100,100, 100, 250,250, 250};
//CGImageRef maskedImageRef = CGImageCreateWithMaskingColors(temp.CGImage, colorMasking); 
CGImageRelease(mask);

//release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel
if (sourceImage != imageWithAlpha) {
    CGImageRelease(imageWithAlpha);
}

UIImage* retImage = [UIImage imageWithCGImage:masked];
CGImageRelease(masked);

return retImage;

}

CGImageRef CopyImageAndAddAlphaChannel(CGImageRef sourceImage) {
CGImageRef retVal = NULL;

size_t width = CGImageGetWidth(sourceImage);
size_t height = CGImageGetHeight(sourceImage);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef offscreenContext = CGBitmapContextCreate(NULL, width, height, 
                                                      8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);

if (offscreenContext != NULL) {
    CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), sourceImage);

    retVal = CGBitmapContextCreateImage(offscreenContext);
    CGContextRelease(offscreenContext);
}

CGColorSpaceRelease(colorSpace);

return retVal;

}

在模拟器上完美运行,但不能在 iPhone 设备上运行:所有图像都被蒙版,但背景为黑色。

于 2010-07-21T12:03:40.577 回答