首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CGImageMaskCreate创建蒙版是全黑的(iphone)

使用CGImageMaskCreate创建蒙版是全黑的(iphone)
EN

Stack Overflow用户
提问于 2009-03-11 01:57:46
回答 3查看 23.4K关注 0票数 14

我正在尝试创建一个由两个现有图像合成的图像蒙版。

首先,我开始创建合成图像,它由一个小图像和一个与背景相同大小的图像组成:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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作为遮罩,它工作得很好,只是当我试图从多个图像中制作它时,它被打破了。

我认为这是一个色彩空间的东西,但似乎无法解决它。任何帮助都是非常感谢的!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-03-11 11:25:22

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

代码语言:javascript
复制
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;
票数 20
EN

Stack Overflow用户

发布于 2011-05-18 19:40:58

代码语言:javascript
复制
- (UIImage *) maskImage:(UIImage *)image {
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    UIImage *maskImage = [UIImage imageNamed:@"MaskFinal.png"];
    CGImageRef maskImageRef = [maskImage CGImage];
    
    // create a bitmap graphics context the size of the image
    CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    
    
    if (mainViewContentContext==NULL)
        return NULL;
    
    CGFloat ratio = 0;
    
    ratio = maskImage.size.width/ image.size.width;
    
    if(ratio * image.size.height < maskImage.size.height) {
        ratio = maskImage.size.height/ image.size.height;
    } 
    
    CGRect rect1 = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
    CGRect rect2 = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};
    
    
    CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
    CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);
    
    
    // Create CGImageRef of the main view bitmap content, and then
    // release that bitmap context
    CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
    CGContextRelease(mainViewContentContext);
    
    UIImage *theImage = [UIImage imageWithCGImage:newImage];
    
    CGImageRelease(newImage);
    
    // return the image
    return theImage;
}
票数 4
EN

Stack Overflow用户

发布于 2010-10-26 23:01:59

必须使用alpha通道创建要被遮罩的图像。Alpha通道可能不是从代码创建的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/633051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档