本篇文章主要介绍了iOS 对view进行截图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本文主要介绍了iOS 对view进行截图的示例代码,分享给大家,具体如下:
需要对WKWebView进行截图,之前用的是下面的方法,高版本的系统是没有问题的,低版本的却截到一张白图
- (UIImage *)convertViewToImage:(UIView *)view{
// 第二个参数表示是否非透明。如果需要显示半透明效果,需传NO,否则YES。第三个参数就是屏幕密度了
UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.bounds.size.width, view.bounds.size.height * 0.8),YES,[UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
查看了之后发现是层级有问题,没有截到最上面的视图,于是改为下面的方法就行了
- (UIImage*)captureView:(UIView *)theView frame:(CGRect)frame{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(theView.bounds.size.width, theView.bounds.size.height*0.8), YES, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *img;
if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0){
for(UIView *subview in theView.subviews){
[subview drawViewHierarchyInRect:subview.bounds afterScreenUpdates:YES];
}
img = UIGraphicsGetImageFromCurrentImageContext();
}else{
CGContextSaveGState(context);
[theView.layer renderInContext:context];
img = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
return img;
}
要注意,frame是不能为空的,而且截的太快的话也会有问题,需要设置afterScreenUpdates为NO,因为设置为YES后,这些方法会等在view update结束在执行,如果在update结束前view被release了,会出现找不到view的问题。另外记得使用UIGraphicsBeginImageContextWithOptions,这样截取出来的是高清图。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
织梦狗教程
本文标题为:iOS 对view进行截图的示例代码
基础教程推荐
猜你喜欢
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- android studio按钮监听的5种方法实例详解 2023-01-12
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- Android多返回栈技术 2023-04-15
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- Flutter手势密码的实现示例(附demo) 2023-04-11
- Android中的webview监听每次URL变化实例 2023-01-23
