-[UIImage drawInRect:blendMode:alpha:]のブレンドモードを活用した色バリエーションの作り方。
こういうのをプログラムで色をつけて

こうする

やり方はシンプル
1. image context を作成
2. 指定の色で塗りつぶし
3. ブレンドモードを指定して、画像を上書き。
4. カレントcontextからUIImageを取り出す。
なお元となる画像はあらかじめ背景を透明にしてアルファチャンネルを持っておく(pngなどを使う)。
コードはこんな感じ。
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor blendingMode:(CGBlendMode)blendMode { UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f); [tintColor setFill]; CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height); UIRectFill(bounds); [self drawInRect:bounds blendMode:blendMode alpha:1.0f]; if (blendMode != kCGBlendModeDestinationIn) [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0]; UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return tintedImage; }