通过另一个图像掩盖图像

Oba*_*aid 3 iphone image masking uiimage

好吧,我想做的是:

  • 给出一个图像,其中该图像中有一个"空白"的圆圈.我想从用户库中获取现有图像,然后将其屏蔽,以便只有该图像的某个部分显示在"空白"图像上.

我尝试了一些屏蔽代码,但它们似乎都是相反的工作...有关如何解决这个问题的任何提示?

Bro*_*olf 5

不幸的是你无法使用CoreAnimation来做这件事(这会让它变得相当简单).查看Apple的CoreAnimation 文档:

iOS注意:作为性能考虑因素,iOS不支持mask属性.

因此,要做到这一点,下一个最好的方法是使用石英2D(如回答这里):

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;
Run Code Online (Sandbox Code Playgroud)