如何更改iphone sdk xcode中的uiimage颜色
我使用不同的图像,我想包括改变颜色选项。但我不能。有人帮我吗?如果您想进行图像着色,请参阅中的UIImage+Tint.m。如果您希望整体更换颜色(例如,将图像视为轮廓,并将整个颜色更改为一种单色),请参见UIImage+Tint.m in.实现此目的的一种方法是降低图像的饱和度,并在图像顶部添加一种与您所需颜色相同的色调 去饱和如何更改iphone sdk xcode中的uiimage颜色,iphone,Iphone,我使用不同的图像,我想包括改变颜色选项。但我不能。有人帮我吗?如果您想进行图像着色,请参阅中的UIImage+Tint.m。如果您希望整体更换颜色(例如,将图像视为轮廓,并将整个颜色更改为一种单色),请参见UIImage+Tint.m in.实现此目的的一种方法是降低图像的饱和度,并在图像顶部添加一种与您所需颜色相同的色调 去饱和 -(UIImage *) getImageWithUnsaturatedPixelsOfImage:(UIImage *)image { const int
-(UIImage *) getImageWithUnsaturatedPixelsOfImage:(UIImage *)image {
const int RED = 1, GREEN = 2, BLUE = 3;
CGRect imageRect = CGRectMake(0, 0, image.size.width*2, image.size.height*2);
int width = imageRect.size.width, height = imageRect.size.height;
uint32_t * pixels = (uint32_t *) malloc(width*height*sizeof(uint32_t));
memset(pixels, 0, width * height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [image CGImage]);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t * rgbaPixel = (uint8_t *) &pixels[y*width+x];
uint32_t gray = (0.3*rgbaPixel[RED]+0.59*rgbaPixel[GREEN]+0.11*rgbaPixel[BLUE]);
rgbaPixel[RED] = gray;
rgbaPixel[GREEN] = gray;
rgbaPixel[BLUE] = gray;
}
}
CGImageRef newImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);
UIImage * resultUIImage = [UIImage imageWithCGImage:newImage scale:2 orientation:0];
CGImageRelease(newImage);
return resultUIImage;
}
如何
//For a UIImageView
yourImageView.image = [self getImageWithUnsaturatedPixelsOfImage:yourImageView.image];
yourImageView.image = [atom getImageWithTintedColor:yourImageView.image withTint:[UIColor redColor] withIntensity:0.7];
//For a UIImage
yourImage = [self getImageWithUnsaturatedPixelsOfImage:yourImage];
yourImage = [atom getImageWithTintedColor:yourImageView.image withTint:[UIColor redColor] withIntensity:0.7];
您可以根据自己的需要更改色调。这是最新也是最简单的方法
theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];
是否要使用颜色“着色”图像?“更改颜色选项”是什么意思?如果整个图像为白色。如果我选择黑色按钮,那么整个图像将是黑色的。。。etcKevin@Thanks分配,现在开始工作了。我可以很容易地改变任何图像的颜色。在使用一些分辨率很高的图像时获得内存警告。有什么想法吗?这里图像显示为灰色,但如果我想用不同的颜色显示,那么该怎么办?如果你已经实现了我的解决方案,那么你所要做的就是这样
yourImage=[atom getImageWithTintedColor:yourImageView.image withTint:[UIColor redColor]withIntensity:0.7]代码>我收到以下错误::CGContextSetFillColorWithColor:无效上下文0x0:CGContextSetBlendMode:无效上下文0x0:CGContextSetAlpha:无效上下文0x0:CGContextFillRects:无效上下文0x0此外,还可以在图像资源条目中将图像标记为模板。然后为包含imageView设置tintColor。
theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];