iOS专题iOS常用功能和UI效果的实现

iOS开发-截屏

2016-09-06  本文已影响584人  胖笋

项目需要做个截屏并保存到相册的功能,于是去网上搜了下,大部分是采用的以下方式:


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);

}

上一篇 下一篇

猜你喜欢

热点阅读