UIImage 遮罩在 iPhone 上提供黑色背景(而不是在模拟器中)

发布于 2024-09-10 14:39:08 字数 869 浏览 9 评论 0原文

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

我有想要遮盖的 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];

}

I've got mask images w/o alpha channel, pure black and white with a black polygone on the center sourrounded by white.

I've got png's image that I'd like to mask. the png have got an alpha channel.
using the method here below : ok on simulator, but background is black on device ...
any idea to fix that ?

- (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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自我难过 2024-09-17 14:39:08

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

Problem Solved :
solution is : the image to be masked MUST be created with an alpha channel.
The Alpha channel may not be created from the code.

栩栩如生 2024-09-17 14:39:08

与以下代码相同

- (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 设备上则不然:所有图像都被遮盖,但背景为黑色。

Same here with the following code

- (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;

}

Works perfectly on simulator but not on iPhone device : all images are masked but with black background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文