use*_*509 3 ios uigraphicscontext swift
我的故事板上有 3 个视图,viewA, viewB, viewC。
我试图屏幕捕获只有两个视图,因为它们出现在屏幕上的当前位置,viewB并且viewC.
问题是,当我渲染它们时,捕获的结果图像显示viewB并且viewC在不正确的位置,视图的位置改变移动左上角 (0, 0),见图像。
如何更正下面的代码,以便我可以使用下面的实现捕获视图viewB并viewC准确地将它们定位在视图上renderInContext?
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0)
self.viewB.layer.renderInContext(UIGraphicsGetCurrentContext()!)
self.viewC.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)
从文档中renderInContext::
在图层的坐标空间中渲染。
每个视图的图层的原点为 0,0,因此它们都出现在左上角。
要解决此问题,您需要在调用renderInContext:.
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0)
let ctx = UIGraphicsGetCurrentContext()
CGContextSaveGState(ctx)
CGContextTranslateCTM(ctx, self.viewB.frame.origin.x, self.viewB.frame.origin.y)
self.viewB.layer.renderInContext(UIGraphicsGetCurrentContext()!)
CGContextRestoreGState(ctx)
CGContextSaveGState(ctx)
CGContextTranslateCTM(ctx, self.viewC.frame.origin.x, self.viewC.frame.origin.y)
self.viewC.layer.renderInContext(UIGraphicsGetCurrentContext()!)
CGContextRestoreGState(ctx)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
427 次 |
| 最近记录: |