改变iphone sdk中的图像颜色 [英] changing the color of image in iphone sdk
本文介绍了改变iphone sdk中的图像颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张图片&我想通过编程来改变图像的颜色。
& 我要更改此图片的颜色
解决方案
更新: b
$ b
使用此方法...
- (UIImage *)imageNamed:(NSString * )name withColor:(UIColor *)color {
//加载图片
UIImage * img = [UIImage imageNamed:name];
//开始一个新的图像上下文,将我们的彩色图像绘制到
UIGraphicsBeginImageContext(img.size);
//获取对我们创建的上下文的引用
CGContextRef context = UIGraphicsGetCurrentContext();
//设置填充颜色
[color setFill];
//翻译/翻转图形上下文(用于从CG * coords转换为UI * coords
CGContextTranslateCTM(context,0,img.size.height);
CGContextScaleCTM context,1.0,-1.0);
//将混合模式设置为彩色刻录,原始图像
CGContextSetBlendMode(context,kCGBlendModeColorBurn);
CGRect rect = CGRectMake 0,0,img.size.width,img.size.height);
CGContextDrawImage(context,rect,img.CGImage);
//设置一个与图像,然后绘制(颜色烧伤)彩色矩形
CGContextClipToMask(context,rect,img.CGImage);
CGContextAddRect(context,rect);
CGContextDrawPath(context,kCGPathFill);
//从我们绘制的图形上下文生成一个新的UIImage
UIImage * coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
返回彩色图像
return coloredImg;
}
使用它像下面...
yourImageView.image = [self imageNamed:@yourImageNamewithColor:[UIColor orangeColor]];
希望对您有帮助.....
I have a image & i want to change the color of that image through programatically.
& I want to change the color of this image
解决方案
UPDATE:
Use this method...
-(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;
}
Use it like bellow...
yourImageView.image = [self imageNamed:@"yourImageName" withColor:[UIColor orangeColor]];
hope this helpful to you.....
这篇关于改变iphone sdk中的图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文