| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
pwmd
10年前发布

IOS截取屏幕到文件中

在这里首先明确,View中显示的东西都在在layer中的。

我们通过renderInContext来渲染layer中的内容

      - (void)viewDidLoad {            [super viewDidLoad];            //截取当前的View            //1.创建图层            UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);            //2.渲染layer            CGContextRef ctr = UIGraphicsGetCurrentContext();            [self.view.layer renderInContext:ctr];            CGContextStrokePath(ctr);            //3.取出image            UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();            //4.关闭图层            UIGraphicsEndImageContext();                        //5.将图片转换成NSData            NSData *data = UIImagePNGRepresentation(imageNew);            [data writeToFile:@"/Users/misaka/Desktop/1.png" atomically:YES];            // Do any additional setup after loading the view, typically from a nib.        }