1.通过扩展实现将UIView转成UIImage
extension UIView {
//将当前视图转为UIImage
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
//将整个视图转为UIImage
let image = UIApplication.shared.keyWindow!.asImage()
2.使用SwViewCapture库实现滚动视图的截屏
http://www.hangge.com/blog/cache/detail_2114.html
安装配置
(1)从 GitHub 上下载最新的代码:https://github.com/startry/SwViewCapture
(2)将下载下来的源码包中 SwViewCapture.xcodeproj 拖拽至你的工程中
(3)工程 -> General -> Embedded Binaries 项,把 framework 添加进来:SwViewCapture.framework
(4)最后,在需要使用 SwViewCapture 的地方 import 进来就可以了。import SwViewCapture
//普通截屏
UIApplication.shared.keyWindow!.swCapture { (capturedImage) -> Void in
self.imageView.image = capturedImage
}
//内容截屏
self.tableView.swContentCapture { (capturedImage) -> Void in
self.imageView.image = capturedImage
}
//滚动截屏(同样可以截出所有内容)
self.tableView.swContentScrollCapture { (capturedImage) -> Void in
self.imageView.image = capturedImage
}
网友评论