Iphone 替换图像中的特定颜色
假设我只想更改给定UIImageView中的特定颜色(每像素)。 如何使用以下代码将所有白色像素更改为暗灰色Iphone 替换图像中的特定颜色,iphone,objective-c,ios,uiimage,uicolor,Iphone,Objective C,Ios,Uiimage,Uicolor,假设我只想更改给定UIImageView中的特定颜色(每像素)。 如何使用以下代码将所有白色像素更改为暗灰色 - (UIImage *) changeColorForImage:(UIImage *)image toColor:(UIColor*)color { UIGraphicsBeginImageContext(image.size); CGRect contextRect; contextRect.origin.x = 0.0f; contextRect.origin.y
- (UIImage *) changeColorForImage:(UIImage *)image toColor:(UIColor*)color {
UIGraphicsBeginImageContext(image.size);
CGRect contextRect;
contextRect.origin.x = 0.0f;
contextRect.origin.y = 0.0f;
contextRect.size = [image size];
// Retrieve source image and begin image context
CGSize itemImageSize = [image size];
CGPoint itemImagePosition;
itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) );
UIGraphicsBeginImageContext(contextRect.size);
CGContextRef c = UIGraphicsGetCurrentContext();
// Setup shadow
// Setup transparency layer and clip to mask
CGContextBeginTransparencyLayer(c, NULL);
CGContextScaleCTM(c, 1.0, -1.0);
CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [image CGImage]);
// Fill and end the transparency layer
const float* colors = CGColorGetComponents( color.CGColor );
CGContextSetRGBFillColor(c, colors[0], colors[1], colors[2], .75);
contextRect.size.height = -contextRect.size.height;
contextRect.size.height -= 15;
CGContextFillRect(c, contextRect);
CGContextEndTransparencyLayer(c);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Now that I know this simple code, I can even create idiotic methods like:
- (UIColor*) oppositeColor:(UIColor*)color {
const float* colors = CGColorGetComponents(color.CGColor);
return [UIColor colorWithRed:1-colors[0] green:1-colors[1] blue:1-colors[2] alpha:1];
}
您是如何解决这个问题的?您可以使用核心图像框架和CIColorCube过滤器来交换色调。