gpt4 book ai didi

ios - 在iphone sdk中改变图像的颜色

转载 作者:可可西里 更新时间:2023-11-01 03:36:16 24 4
gpt4 key购买 nike

我有一张图片,我想通过编程更改该图片的颜色。

enter image description here

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

enter image description here

最佳答案

更新:

使用这个方法...

-(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]];

关于ios - 在iphone sdk中改变图像的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14292212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com