屏蔽UIImage并设置彩色图像

时间:2012-10-23 07:39:43

标签: ios uiimage uigesturerecognizer uicolor

我正在使用一个与gestureRecognizer配合使用的应用程序。
使用手势可以选择UIImage(例如rectangle.png),UIPopoverView可以通过选择所选图像的颜色来改变图像的颜色。

此图像位于UIImageView中,我认为最佳解决方案是屏蔽该图像并使用dame大小和框架设置彩色图像。

这是正确的方法吗?我该如何优化我的方法? 哪个可能是此要求的最佳实践?

1 个答案:

答案 0 :(得分: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;
}