0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

色だけを指定してUIImageを作る

Posted at

検索しても最近は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]];
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?