我正在开发一个与 gestureRecognizer 配合使用的应用程序。
使用手势可以选择 UIImage(例如 rectangle.png),并且可以使用 UIPopoverView 通过为所选图像选择颜色来更改该图像的颜色。
这张图片位于 UIImageView 中,我认为最好的解决方案是屏蔽该图片并设置一个具有相同大小和框架的彩色图片。
这是正确的方法吗?我怎样才能优化我的方法? 哪个可能是满足此要求的最佳实践?
最佳答案
(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;
}
关于ios - 屏蔽 UIImage 并改为设置彩色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13025634/