通过另一个图像掩盖图像 [英] mask image via another image
本文介绍了通过另一个图像掩盖图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧我想做的是:
- 给出一张图片,其中该图片中有一个空白的圆圈。我想从用户库中获取现有图像,然后将其屏蔽,以便只有该图像的某个部分显示在空白图像上。
我尝试了一些屏蔽代码,但它们似乎都在反过来......有关如何解决这个问题的任何提示吗?
I have tried a few masking code but they all seem to work the other way around ... any tips on how to tackle this?
推荐答案
不幸的是,您无法使用 CoreAnimation 来执行此操作(这会使其变得相当简单)。
查看Apple的CoreAnimation 文档 :
Unfortunately you can't use CoreAnimation to do this (which would make it rather easy). Looking at Apple's CoreAnimation documentation:
iOS注意:作为性能考虑,iOS不支持掩码属性。
因此,下一个最佳方法是使用 Quartz 2D (回答在其他地方:
Therefore the next best way to do this is to use Quartz 2D (as answered here):
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;
这篇关于通过另一个图像掩盖图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文