UIGraphicsBeginImageContext 获取图片、截图
来源:互联网 发布:与吾同在 知乎 编辑:程序博客网 时间:2024/05/16 07:11
通过UIGraphicsBeginImageContext可以将一张图片画到指定位置,也可以通过该方法来截取屏幕固定区域的图片,具体实现如下:
1: 画图
func drawImage(image: UIImage) -> UIImage { UIGraphicsBeginImageContext(CGSizeMake(100, 100)) image.drawInRect(CGRectMake(0, 0, 100, 100)) let finalImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return finalImage }
2: 截图
func getScreenShot() -> UIImage { UIGraphicsBeginImageContext(view.bounds.size) view.layer.renderInContext(UIGraphicsGetCurrentContext()!) let finalImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return finalImage }
通过上面的方法即可完成截图和画图。但是在retain屏幕中,以上我们获取的图片会很模糊,严重失真,这是因为我们获取的图片的scale是1,而在retain中,iPhone6及之前是2x,plus之后是3x,所以会发生图片失真的情况。
3: 解决方法
func getScreenShot() -> UIImage { UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.mainScreen().scale) view.layer.renderInContext(UIGraphicsGetCurrentContext()!) let finalImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return finalImage }
使用UIGraphicsBeginImageContextWithOptions函数,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,这样使用,解决问题!
0 0
- UIGraphicsBeginImageContext 获取图片、截图
- IOS UIGraphicsBeginImageContext 截图不清晰
- 图片处理(UIGraphicsBeginImageContext)
- UIGraphicsBeginImageContext 生成图片模糊
- UIGraphicsBeginImageContext
- UIGraphicsBeginImageContext
- UIGraphicsBeginImageContext
- UIGraphicsBeginImageContext
- UIGraphicsBeginImageContext
- VC获取窗口图片(截图)
- VC获取窗口图片(截图)
- 获取拍照图片、获取图库图片、共享View的截图
- UIGraphicsBeginImageContext和UIGraphicsBeginImageContextWithOptions实现iOS中的截图功能
- UIGraphicsBeginImageContext和UIGraphicsBeginImageContextWithOptions实现iOS中的截图功能
- UIGraphicsBeginImageContext和UIGraphicsBeginImageContextWithOptions实现iOS中的截图功能
- ios--图片处理(修改、保存)UIGraphicsBeginImageContext
- ios--图片处理(修改、保存)UIGraphicsBeginImageContext
- UIGraphicsBeginImageContext 生成图片字体模糊问题
- 详细介绍JSP技术的两种架构模型
- Angular 动画
- [MD]模仿百度手机助手动态折线图/MPAndroidCharts实现(二)
- js的HashMap实现
- webpy
- UIGraphicsBeginImageContext 获取图片、截图
- Go Reflect
- copy文件命令
- 架构之路(2):性能
- 搭建cocos 开发环境中的那些坑
- Android源码下载
- 经济学常识
- 总结
- UTF8转NSString+UIControl响应事件