项目需要做个截屏并保存到相册的功能,于是去网上搜了下,大部分是采用的以下方式:
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil,nil);
不过要做的项目需要截屏的是播放视频界面,这种方式只能截取UIkit里的view,当然还有更麻烦的用opengl的函数来截屏,但本着简单直观的原则,内心不是很情愿用。好在简单方法也还是有,需要iOS7+
- (void)snapshotScreenWithView:(UIView *)sView{
UIGraphicsBeginImageContextWithOptions(sView.frame.size, NO, [UIScreen mainScreen].scale);
[sView drawViewHierarchyInRect:sView.frame afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil,nil);
}