其实所谓的截频,就是将我们看到的转化为图片而已,在这里要用到的就是图形上下文啦!
两种方法都可以实现,但是第二种更好一些,第一种利用渲染完成但是可能会出错,系统给了如下警告:
* WARNING: currently this method does not implement the full
* CoreAnimation composition model, use with caution. */
//目前这种方法没有实现完整的CoreAnimation组合模型,谨慎使用
//- (void)renderInContext:(CGContextRef)ctx;
直接上代码:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
[self screenShot];
}
- (void)screenShot {
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"%@",image);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 100, 100)];
imageView.backgroundColor = [UIColor whiteColor];
imageView.image = image;
[self.view addSubview:imageView];
self.view.backgroundColor = [UIColor cyanColor];
//这样写易看出效果,简单粗暴,哈哈
}
- (UIImage *)getSnapshotImage {
//绘制图像开始
UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)), NO, 1);
[self.view drawViewHierarchyInRect:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) afterScreenUpdates:NO];
UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();//结束
NSLog(@"%@",image);
return image;
}