ios - 屏蔽 UIImage 并改为设置彩色图像

标签 ios uiimage uigesturerecognizer uicolor

我正在开发一个与 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/

相关文章:

ios - 与 Apple Watch 共享来自 iOS 的 AsyncStorage React native 应用程序

ios - 从 UIImage PNG 文件中移除透明度(非字母值)

ios - 我们如何获取在iPhone屏幕上绘制的字母坐标

ios - 如何远程运行 xcode 机器人

ios - Google plus 登录在 iOS 应用程序中不起作用

ios - 使用 PDF 的 UIButton 或 UIImageView 的矢量插图

ios - 在选项卡栏 Controller 中为导航 Controller 禁用和启用向后滑动的正确方法

ios - 需要应用 UIRotationGestureRecognizer 后跟 UILongPressGestureRecongnizer

android - 使用 gradleFX 为多个客户构建 AIR 移动项目

objective-c - 比较CGPoint和图像中心属性