objective-c - 使用 CGImageMaskCreate 创建蒙版是全黑的(iphone)

标签 objective-c iphone cocoa-touch core-graphics image-manipulation

我正在尝试从两个现有图像的合成中创建一个图像 mask 。

首先,我开始创建合成图,其中包含一个小图像作为掩蔽图像,以及一个较大的图像,其大小与背景相同:

UIImage *baseTextureImage = [UIImage imageNamed:@"background.png"];
UIImage *maskImage = [UIImage imageNamed:@"my_mask.jpg"];
UIImage *shapesBase = [UIImage imageNamed:@"largerimage.jpg"];
UIImage *maskImageFull;

CGSize finalSize = CGSizeMake(480.0, 320.0);
UIGraphicsBeginImageContext(finalSize);
[shapesBase drawInRect:CGRectMake(0, 0, 480, 320)];
[maskImage drawInRect:CGRectMake(150, 50, 250, 250)];
maskImageFull = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我可以输出这个 UIImage (MaskImageFull),它看起来不错。它是一个全尺寸背景尺寸,它有一个白色背景,我的蒙版对象是黑色的,在屏幕上的正确位置。

然后我通过这个传递 MaskImageFull UIImage:

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);
UIImage *retImage= [UIImage imageWithCGImage:masked];

问题是 retImage 是全黑的。如果我发送一个预制的 UIImage 作为蒙版,它工作正常,只是当我尝试从多个图像中创建它时,它会中断。

我认为这是色彩空间的问题,但似乎无法修复它。非常感谢任何帮助!

最佳答案

我用 CGImageCreateWithMask 尝试了同样的事情,得到了同样的结果。我找到的解决方案是改用 CGContextClipToMask:

CGContextRef mainViewContentContext;
CGColorSpaceRef colorSpace;

colorSpace = CGColorSpaceCreateDeviceRGB();

// create a bitmap graphics context the size of the image
mainViewContentContext = CGBitmapContextCreate (NULL, targetSize.width, targetSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

// free the rgb colorspace
CGColorSpaceRelease(colorSpace);    

if (mainViewContentContext==NULL)
    return NULL;

CGImageRef maskImage = [[UIImage imageNamed:@"mask.png"] CGImage];
CGContextClipToMask(mainViewContentContext, CGRectMake(0, 0, targetSize.width, targetSize.height), maskImage);
CGContextDrawImage(mainViewContentContext, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), self.CGImage);


// Create CGImageRef of the main view bitmap content, and then
// release that bitmap context
CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);

// convert the finished resized image to a UIImage 
UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
// image is retained by the property setting above, so we can 
// release the original
CGImageRelease(mainViewContentBitmapContext);

// return the image
return theImage;

关于objective-c - 使用 CGImageMaskCreate 创建蒙版是全黑的(iphone),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/633051/

相关文章:

ios - 以编程方式将 UIButtons 添加到 UICollectionView 单元格的 uiimage

iphone - 在启用 ARC 的 iOS5 应用程序中替换 NSZoneMalloc 的使用

php - 让 json_encode 不转义 html 实体

cocoa-touch - 可以在 VOIP 应用程序中将多个 tcp 套接字标记为 kCFStreamNetworkServiceTypeVoIP 吗?

ios - 如何保持 UIButton 突出显示直到第二次触摸?

iphone - 如何在模态视图解除时传递对象

objective-c - NSOutlineView,setFloatsGroupRows : not working

ios - 角标(Badge)推送增量仅适用于 xcode

iphone - 闪烁隐藏和使用 block

iphone - CoreData一对多和逆关系问题