伸縮可能な UIImage に色を付けたいのですが、うまくいきません。この投稿の助けを借りて: UIImage の色を変更する方法 UIImageを正常に色付けしましたが、伸縮可能にすると、「新しい」フレームに色が設定されません。
カテゴリを使用して色を設定する方法は次のとおりです。
+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
UIImage *image = [UIImage imageNamed:name];
if (color == nil)
return image;
CGRect rect = CGRectMake(0.f, 0.f, image.size.width, image.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, rect, image.CGImage);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [UIImage imageWithCGImage:img.CGImage
scale:1.0
orientation:UIImageOrientationDownMirrored];
}
どんな提案でも大歓迎です...