iOS绘图:快照、滤镜与视图绘制全解析
1. 视图快照
在iOS开发中,视图快照是一个非常有用的功能。可以通过调用 UIView
实例方法 drawHierarchy(in:afterScreenUpdates:)
将整个视图(从单个按钮到整个界面)绘制到当前图形上下文中,得到的结果是原始视图的快照,它看起来和原始视图一样,但本质上只是一个位图图像,是轻量级的视觉副本。
let im = r.image { _ in
UIImage(cgImage: marsLeft!,
scale: mars.scale,
orientation: mars.imageOrientation).draw(at:CGPoint(0,0))
UIImage(cgImage: marsRight!,
scale: mars.scale,
orientation: mars.imageOrientation).draw(at:CGPoint(sz.width,0))
}
获取视图快照还有更快的方法,即使用 UIView
(或 UIScreen
)实例方法 snapshotView(afterScreenUpdates:)
,其结果是一个 UIView
,而不是 UIImage
,类似于一个只能绘制一张图像(即快照)的 UIIm