【发布时间】:2018-03-30 05:01:32
【问题描述】:
我有一个在滚动视图上的绘图视图。绘图完成后,我需要将要上传到服务器的绘图屏幕截图。
我正在使用 UIBezeir 路径在视图上绘制。
let path = UIBezierPath()
for i in self.drawView.path{
path.append(i)
}
self.drawView.path 是一个 NSArray,包含绘图的所有 bezeir 路径。
但是当我使用这条路径的边界框并获取坐标的最大值和最小值并尝试捕获屏幕截图时,我得到了这个
var rect:CGRect = CGRect(x: path.bounds.minX, y: path.bounds.minY, width: path.bounds.maxX, height: path.bounds.maxY)
我也尝试给出路径本身的边界
let rect:CGRect = CGRect(x: path.bounds.origin.x - 5, y: path.bounds.origin.y - 5, width: path.bounds.size.width + 5, height: path.bounds.size.height + 5)
仅供参考,我尝试使用此矩形并创建一个视图(带有边框层的清晰颜色)并将其放置在绘图上,它工作得很好但是当我尝试捕获图像时它超出了边界
这是我用来截屏的函数
func imgScreenShot(bounds:CGRect) -> UIImage{
let rect: CGRect = bounds
self.drawView.isOpaque = false
self.drawView.backgroundColor = UIColor.clear
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
var context: CGContext? = UIGraphicsGetCurrentContext()
if let aContext = context {
self.drawView.layer.render(in: aContext)
}
var capturedImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//let finalImage = scaleImage(image: capturedImage)
return capturedImage!
}
我也尝试使用此功能获取 UIView
let vw = self.drawView.resizableSnapshotView(from: rect, afterScreenUpdates: true, withCapInsets: UIEdgeInsets.zero)
这给了我一个完美的 UIView 与绘图,但是当我再次尝试使用提供视图边界的函数将 UIView 转换为 UIImage 时,我得到一个空白图像。
任何人都可以提出我做错了什么或任何其他解决方案来解决这个问题,图像的边界正好从绘图的边界开始
let vw = self.drawView.resizableSnapshotView(from: rect2, afterScreenUpdates: true, withCapInsets: UIEdgeInsets.zero)
vw?.frame = CGRect(x: 50, y: 50, width: 100, height: 100)
vw?.layer.borderColor = UIColor.red.cgColor
vw?.layer.borderWidth = 1
self.drawView.addSubview(vw!)
let image = vw?.snapshotImage
let imgView = UIImageView(frame: CGRect(x: 250, y: 50, width: 100, height: 100))
imgView.layer.borderColor = UIColor.gray.cgColor
imgView.layer.borderWidth = 1
self.drawView.addSubview(imgView)
【问题讨论】:
标签: ios swift xcode layer uibezierpath