Imageview ios中的遮罩和反向遮罩

我有一个Imageview包含图像。 和一个包含兔子形状的面具形状。 我有一个代码给出了下面的结果。

- (UIImage*)mynewmaskImage:(UIImage *)image withMask:(UIImage *)maskImage { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGImageRef maskImageRef = [maskImage CGImage]; // create a bitmap graphics context the size of the image CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL,320, 380, 8, 0, colorSpace,(CGBitmapInfo) kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorSpace); if (mainViewContentContext==NULL) return NULL; CGFloat ratio = 0; ratio = 320/ image.size.width; if(ratio * image.size.height < 380) { ratio = 380/ image.size.height; } CGRect rect1 = {{0, 0}, {320,380}}; CGRect rect2 = {{-((image.size.width*ratio)-320)/2 , -((image.size.height*ratio)-380)/2}, {image.size.width*ratio, image.size.height*ratio}}; // CGContextDrawImage(mainViewContentContext, rect2, image.CGImage); CGContextClipToMask(mainViewContentContext, rect1, maskImageRef); CGContextDrawImage(mainViewContentContext, rect2, image.CGImage); CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext); CGContextRelease(mainViewContentContext); UIImage *theImage = [UIImage imageWithCGImage:newImage]; CGImageRelease(newImage); // return the image return theImage;} 

上面的代码给出了这个结果。 在这里输入图像说明

但是我想要下面的结果(如反向遮罩)。 在这里输入图像说明

怎么可能,请帮帮我。 谢谢。

你应该看看blendMode。 试试像这样的:

 [rabbitImage drawInRect:rect blendMode:kCGBlendModeDestinationOut alpha:1.0];