5

我正在创建一个应用程序,该应用程序使用UIImagePickerController自定义叠加层向用户展示相机,其中包括相机“视图”本身的两个网格/图案之一。

网格本身UIImageView是添加到叠加层中的 .png 文件,它们非常复杂,所以我真的很想避免在代码中绘制网格,即使这会给我的问题提供干净而简单的答案.

我希望能够提供各种颜色的网格。显而易见的解决方案是创建更多不同颜色的 .png 图像,但是对于每种颜色,必须有四个单独的图像(每个网格的常规图像和视网膜图像),这样很快就会增加很多资源。

我认为理想的解决方案是我只创建白色/灰色的网格,然后对其应用色调以适当地着色。

那可能吗?还是我需要寻找替代解决方案?

4

1 回答 1

8

感谢 Ananth 将我指向iPhone - 你如何为图像着色?

我已按照问题中的建议将此方法添加到我的代码中,并在 willc2 的答案中进行了修改:

-(UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor {
    UIGraphicsBeginImageContext(baseImage.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);

    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSaveGState(ctx);
    CGContextClipToMask(ctx, area, baseImage.CGImage);
    [theColor set];
    CGContextFillRect(ctx, area);
    CGContextRestoreGState(ctx);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextDrawImage(ctx, area, baseImage.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

...而且我得到的正是我所追求的。

于 2012-06-29T08:35:19.757 回答