taoru's memo

Objective-c,cocos2d,など開発についてのメモ(andoroidも少しだけ)

UIViewをUIImageに変換する

拡張子が.pngの場合、UIImageに渡すファイル名では拡張子を省略できる(iOS 4以降)

UIImage Class Reference
On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension

UIImage *image = [UIImage imageNamed:@"hoge"];

UIViewをUIImageに変換する

- (UIImage *)convertToUIImage
{
    UIImage *image;
     
    // UIViewのサイズの 画像コンテキストを開始
    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, [UIScreen mainScreen].scale);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:context];
    image = UIGraphicsGetImageFromCurrentImageContext();
    
    // 画像コンテキストを終了
    UIGraphicsEndImageContext();
    
    return image;
}
QLOOKアクセス解析