画像があり、その画像の色をプログラムで変更したい。
&この画像の色を変えたい
アップデート:
この方法を使用して...
-(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]];
CoreImage カラー フィルターは、この種のタスクに最適です。Core Graphic クラス (CG...) を使用するよりも、少し簡単だと思います。使用している画像の RGB およびアルファ特性を調整できるようにすることで機能します。 QRコードの白い背景を色付きに変更します。あなたの場合、白のRGBAは(1,1,1,1)です。色を逆にする必要があると思います。Apple の CI ドキュメントを確認してください。利用可能なフィルターは数十あります。CIColorMatrix はその 1 つにすぎません。
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);
ここで答えたようにiPhone - どのように画像に色を付けますか? 私の意見では、iOS 7 から画像に色を付ける最良の方法は、
myImageView.image = [[UIImage imageNamed:@"myImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
次に、imageViewのtintColorまたは画像を含むものを変更します。