LoginSignup
27
28

More than 5 years have passed since last update.

【画像を保存]UIViewを画像で保存する。

Posted at

UIView を見た目そのまま画像として保存したい。

saveImage.m
//Core Graphicsを使います
#import <QuartzCore/QuartzCore.h>
//保存したい UIView のインスタンス view があるとして、それを保存する方法は次の通り。
UIGraphicsBeginImageContext(view.frame.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // PNGの場合(view.alphaで指定した透明度も維持されるみたい)
    NSData *dataSaveImage = UIImagePNGRepresentation(image);

    // JPEGの場合
    //NSData *dataSaveImage = UIImageJPEGRepresentation(image, 1.0);    

    // Documentsディレクトリに保存
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    [dataSaveImage writeToFile:[path stringByAppendingPathComponent:@"test.png"] atomically:YES];

27
28
1

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
27
28