6

我有一张图片,我想以编程方式更改该图片的颜色。

在此处输入图像描述

&我想改变这张图片的颜色

在此处输入图像描述

4

5 回答 5

13

更新:

用这个方法...

-(UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color {
    // load the image
    
    UIImage *img = [UIImage imageNamed:name];
    
    // begin a new image context, to draw our colored image onto
    UIGraphicsBeginImageContext(img.size);
    
    // get a reference to that context we created
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // set the fill color
    [color setFill];
    
    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, img.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    // set the blend mode to color burn, and the original image
    CGContextSetBlendMode(context, kCGBlendModeColorBurn);
    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
    CGContextDrawImage(context, rect, img.CGImage);
    
    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
    CGContextClipToMask(context, rect, img.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);
    
    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //return the color-burned image
    return coloredImg;
}

像下面这样使用它...

yourImageView.image = [self imageNamed:@"yourImageName" withColor:[UIColor orangeColor]];
于 2013-01-12T10:04:01.517 回答
3

这是斯威夫特版本:

extension UIImage {

    func colorizeWith(color: UIColor) -> UIImage {
        UIGraphicsBeginImageContext(self.size)
        let context = UIGraphicsGetCurrentContext()
        color.setFill()
        CGContextTranslateCTM(context, 0, self.size.height)
        CGContextScaleCTM(context, 1.0, -1.0)

        // set the blend mode to color burn, and the original image
        CGContextSetBlendMode(context, kCGBlendModeNormal);
        let rect = CGRectMake(0, 0, self.size.width, self.size.height);
        CGContextDrawImage(context, rect, self.CGImage);

        // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
        CGContextClipToMask(context, rect, self.CGImage);
        CGContextAddRect(context, rect);
        CGContextDrawPath(context,kCGPathFill);

        // generate a new UIImage from the graphics context we drew onto
        let coloredImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        //return the color-burned image
        return coloredImage;
    }

}
于 2014-12-09T23:52:53.060 回答
2

你可以试试Ankish Jain 的回答,它对我有用。

theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];
于 2015-08-25T15:40:12.587 回答
1

CoreImage 颜色过滤器非常适合此类任务 - 我发现它们比使用 Core Graphic 类 (CG...) 稍微简单一些:它们的工作原理是允许您调整我一直在使用的图像的 RGB 和 Alpha 特性将 QRCode 的白色背景更改为彩色。在你的情况下,白色的 RGBA 是 (1,1,1,1) 我相信你必须反转颜色。只需查看 Apple 的 CI 文档,就有几十个可用的过滤器 CIColorMatrix 只是其中之一。

CIImage *beginImage = [CIImage imageWithCGImage:image.CGImage];

CIContext *context = [CIContext contextWithOptions:nil];

CIFilter *filtercd = [CIFilter filterWithName:@"CIColorMatrix"  //rgreen
                                keysAndValues: kCIInputImageKey, beginImage, nil];
[filtercd setValue:[CIVector vectorWithX:0 Y:1 Z:1 W:0] forKey:@"inputRVector"]; // 5
[filtercd setValue:[CIVector vectorWithX:1 Y:0 Z:1 W:0] forKey:@"inputGVector"]; // 6
[filtercd setValue:[CIVector vectorWithX:1 Y:1 Z:0 W:0] forKey:@"inputBVector"]; // 7
[filtercd setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"]; // 8
[filtercd setValue:[CIVector vectorWithX:1 Y:1 Z:0 W:0] forKey:@"inputBiasVector"]; 
CIImage *doutputImage = [filtercd outputImage];


CGImageRef cgimgd = [context createCGImage:doutputImage fromRect:[doutputImage extent]];
UIImage *newImgd = [UIImage imageWithCGImage:cgimgd];

filterd.image = newImgd;

CGImageRelease(cgimgd);
于 2014-02-14T06:04:53.330 回答
0

正如我在这里回答的那样 iPhone - 你如何为图像着色?在我看来,从 iOS 7 为图像着色的最佳方法是使用

myImageView.image = [[UIImage imageNamed:@"myImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

然后更改 imageView 的 tintColor 或包含图像的任何内容。

于 2015-05-19T09:23:09.740 回答