iOS7.0之前的系统,可以通过以下代码实现截屏功能。
- (UIImage *)captureImageFromView:(UIView *)view{
CGRect screenRect = view.bounds;
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
[view.layer renderInContext:contextRef];
UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return returnImage;
// UIImage.size == view.bounds.size
}
iOS7.0之后,系统中封装了截屏的方法
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates
方法调用示例
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(100, 300, 200, 200)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *captureView = [redView snapshotViewAfterScreenUpdates:YES];
[self.view addSubview:captureView];
// captureView.frame == (0,0,200,200)
效果图如下:
代码地址:
https://github.com/FlyingKuiKui/ScreenShots.git
包含获取屏幕快照的方式截图