#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //开启位图上下文
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
    //获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //把控件上的图层渲染到上下文(图层只能渲染不能绘制)
    [self.view.layer renderInContext:ctx];
    //生产一张图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    //把图片转化成二进制PNG
    NSData *imageData = UIImagePNGRepresentation(image);
    //写入到桌面
    [imageData writeToFile:@"/Users/zhaotian/Desktop/view.png" atomically:YES];
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  

posted on 2016-06-26 16:33  良小辰  阅读(127)  评论(0编辑  收藏  举报