【问题标题】:Masking an UIImage and set colored image instead屏蔽 UIImage 并设置彩色图像
【发布时间】:2012-10-23 07:39:42
【问题描述】:
我正在开发一个与gestureRecognizer 配合使用的应用程序。
使用手势可以选择 UIImage(例如 rectangle.png),并且可以使用 UIPopoverView 通过选择所选图像的颜色来更改该图像的颜色。
此图像位于 UIImageView 中,我认为最好的解决方案是屏蔽该图像并设置一个具有 dame 大小和框架的彩色图像。
这是正确的方法吗?如何优化我的方法?
哪个可能是此要求的最佳实践?
【问题讨论】:
标签:
ios
uiimage
uigesturerecognizer
uicolor
【解决方案1】:
(UIImage *)maskImage:(UIColor *)maskColor
{
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextClipToMask(context, rect, self.CGImage);
CGContextSetFillColorWithColor(context, maskColor.CGColor);
CGContextFillRect(context, rect);
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return smallImage;
}