CGContextClipToMask返回空白图像 [英] CGContextClipToMask returning blank image
问题描述
我是Quartz的新手。我有2张图像,一个背景和一个具有剪切形状的蒙版,我想将其放置在背景上以剪切出一部分。生成的图像应为切口的形状。这是我的面具(中间的形状是0 alpha):
I'm new to Quartz. I have 2 images, a background, and a mask with cutout shape that I want to lay over the background in order to cut out a section. The resulting image should be the shape of the cutout. This is my mask (the shape in the middle is 0 alpha):
这是我的代码:
UIView *canvas = [[[sender superview] subviews] objectAtIndex:0];
UIGraphicsBeginImageContext(canvas.bounds.size);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef cgContext = CGBitmapContextCreate(NULL, canvas.bounds.size.width, canvas.bounds.size.height, 8, 0, colourSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colourSpace);
CGImageRef maskImage = [[UIImage imageNamed:@"Mask.png"] CGImage];
CGContextClipToMask(cgContext, CGRectMake(0, 0, canvas.frame.size.width, canvas.frame.size.height), maskImage);
CGImageRef maskedImageRef = CGBitmapContextCreateImage(cgContext);
CGContextRelease(cgContext);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];
UIGraphicsEndImageContext();
[button setBackgroundImage:maskedImage forState:UIControlStateNormal];
除了什么都没有出现...知道我要去哪里了吗?非常感谢。
Except nothing appears... Any idea where I'm going wrong? Many thanks.
推荐答案
用蒙版剪切上下文后,您实际上应该在该上下文上绘制一些东西。
After you clip context with mask you should actually draw something on that context.
此外:
- 您应该使用
UIGraphicsBeginImageContextWithOptions
支持缩放 - 您正在创建两个上下文,一个使用
UIGraphicsBeginImageContext
,另一个使用CGBitmapContextCreate
。一个就足够了:) - 您应该翻转蒙版的Alpha,因为根据文档
,蒙版的源样本确定剪切区域的哪些点已更改
。换句话说,透明将变得透明
- you should use
UIGraphicsBeginImageContextWithOptions
to support scale factor. - you're creating two context, one with
UIGraphicsBeginImageContext
and the other withCGBitmapContextCreate
. One is enough :) - you should "flip" alpha of your mask, because according to docs
The source samples of mask determine which points of the clipping area are changed
. The other words transparent will become transparent
简单的示例代码,在其中我将图像剪辑在imageView中,然后将其重新设置:
Simple example code, where I clip image inside imageView and then set it back:
UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, self.imageView.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGImageRef maskImage = [[UIImage imageNamed:@"mask.png"] CGImage];
CGContextClipToMask(context, self.imageView.bounds, maskImage);
CGContextTranslateCTM(context, 0.0, self.imageView.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
[[self.imageView image] drawInRect:self.imageView.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
self.imageView.image = image;
使用 CGImageCreateWithMask
的示例:
UIImage *image = [UIImage imageNamed:@"image"];
CGImageRef originalMask = [UIImage imageNamed:@"mask"].CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(originalMask),
CGImageGetHeight(originalMask),
CGImageGetBitsPerComponent(originalMask),
CGImageGetBitsPerPixel(originalMask),
CGImageGetBytesPerRow(originalMask),
CGImageGetDataProvider(originalMask), nil, YES);
CGImageRef maskedImageRef = CGImageCreateWithMask(image.CGImage, mask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef scale:image.scale orientation:image.imageOrientation];
CGImageRelease(mask);
CGImageRelease(maskedImageRef);
这篇关于CGContextClipToMask返回空白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!