2014年3月9日日曜日

[iOS]UIColorからUIImageを作成する

stackoverflow.comより
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [self createImageFromUIColor:[UIColor blackColor]];
}

- (UIImage *)createImageFromUIColor:(UIColor *)color
{
    //1x1のBitmapを作成する
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    //Bitmapを塗りつぶし
    CGContextSetFillColorWithColor(contextRef, [color CGColor]);
    CGContextFillRect(contextRef, rect);
    //UIImageに変換
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
};

0 件のコメント:

コメントを投稿