ios - 通过 mask 更改 UIImage 的颜色并将该图像替换为彩色图像

标签 ios uiimage cgcolor

在一个类中,我有一个由五个 UIImage 组成的数组,例如 tiangle.png 和 Circle.png。

在当前类(class)中,我有一个颜色列表。单击 UIImage 和单击一种颜色后,可以更改当前 UIImage 的颜色。 这将通过屏蔽图像、设置其颜色并将旧的 UIImage 替换为所选颜色的新 UIImage 来实现。

但是方法有问题,应该改变颜色:

- (void) changeColor: (UITapGestureRecognizer*) gestureRecognizer{

    UIGraphicsBeginImageContext(test.newView.image.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGPoint loc = [gestureRecognizer locationInView:self.view];
    CGContextSetFillColorWithColor(context, [[self colorOfPoint:loc]CGColor]);
    CGContextTranslateCTM(context, 0, test.newView.image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGRect rect = CGRectMake(test.newView.frame.origin.x,test.newView.frame.origin.x,   test.newView.image.size.width,test.newView.image.size.height);
    CGContextSetBlendMode(context, kCGBlendModeColorBurn);

    CGContextClipToMask(context, rect, test.newView.image.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context, kCGPathFill);

    CGImageRef imgRef = CGBitmapContextCreateImage(context);
    UIImage* img = [UIImage imageWithCGImage:imgRef];
    CGImageRelease(imgRef);
    CGContextRelease(context);
    UIGraphicsEndImageContext();
    test.newView.image = img;

}

唯一发生的事情是单击的 UIImage 将变得不透明。
在这种情况下,保存 UIImage 的 imageview 不会被删除。

最佳答案

我不确定使用 CGBitmapContextCreateImage() 调用获取 imageRef 在 UIGraphics 图像上下文中是否合适。我见过的每个例子都使用

  UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

在关闭图像上下文之前抓取图像。你可以尝试一下。

关于ios - 通过 mask 更改 UIImage 的颜色并将该图像替换为彩色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13012331/

相关文章:

ios - NSError 版本 : message sent to deallocated instance

ios - 多次触发 ALAssetLibraryChangedNotification

arrays - 使用未解析的标识符 'WeekOne'

ios - 如何从 RGB 值中获取 CGColor?

ios - XCode -> 带有下划线的build设置名称

ios - Apple 是否强制要求通过 SSL 进行授权?

iphone - 如何将颜色从一种颜色空间转换为另一种颜色空间?

ios - 新iPhone,无法进行测试吗?

ios - 将图像重新分配给 UIImageView 时图像质量损失