Viewイメージをキャプチャーし、PNG画像で保存する

画面に表示されていないビューのキャプチャーを行う。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// UIImage を PNG画像データへ変換する
+ (BOOL)saveUIImage2PNG:(UIImage *)image FileName:(NSString *)fileName
{
    NSData *data = UIImagePNGRepresentation(image);  
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@.png", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"], fileName];  
    NSLog(@"%@", pngFilePath);
 
    BOOL result;
    if ([data writeToFile:pngFilePath atomically:YES]) {
        result = YES;
    } 
    else {
        result = NO;
    }
 
    return result;
}
 
// Viewイメージをキャプチャーする
// 備考: 画面に表示していなくてもキャプチャーできる
+ (UIImage *)captureViewImage:(UIView *)view 
{   
    UIImage *capture; 
 
    UIGraphicsBeginImageContext(view.bounds.size);
 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    capture = UIGraphicsGetImageFromCurrentImageContext();
 
    UIGraphicsEndImageContext();
 
    return capture;
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です