検索しても最近はswiftの記事ばかりがヒットしてちょっと辛い…。
動機
ダミーイメージとして色、サイズだけを指定してUIImageを作りたかった。
やり方
毎度のことながらStack Overflowにお世話になります。
ios - Creating a UIImage from a UIColor to use as a background image for UIButton - Stack Overflow
CGRectの部分で画像サイズを指定、引数のcolorでUIColorを指定するだけでUIImageが返ってきます。
+ (UIImage *)imageFromColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UIImage *cloud_image = [self imageFromColor:[UIColor whiteColor]];