我正在尝试为我的用户提供他们在 UIImage 上触摸的点的精细选择。 我在左上角有一个放大方 block ,以 2 倍缩放显示它们接触的位置。它运行良好。
我正在尝试在放大区域的中心添加一个“十字准线”以使选择更清晰。
下面的代码没有一行是可见的。
//Bulk of the magifying code
public override func drawRect(rect: CGRect) {
let context: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextScaleCTM(context, 2, 2)
CGContextTranslateCTM(context, -self.touchPoint.x, -self.touchPoint.y)
drawLine(context)
self.viewToMagnify.layer.renderInContext(context)
}
//Code for drawing horizontal line of crosshair
private func drawLine(ctx: CGContext) {
let lineHeight: CGFloat = 3.0
let lineWidth: CGFloat = min(bounds.width, bounds.height) * 0.3
let horizontalPath = UIBezierPath()
horizontalPath.lineWidth = lineHeight
let hStart = CGPoint(x:bounds.width/2 - lineWidth/2, y:bounds.height/2)
let hEnd = CGPoint(x:bounds.width/2 + lineWidth/2,y:bounds.height/2)
horizontalPath.moveToPoint(hStart)
horizontalPath.addLineToPoint(hEnd)
UIColor.whiteColor().setStroke()
horizontalPath.stroke()
}
正在绘制的线可能太小或不在我预期的位置。
我尝试了其他画线的方法,比如使用 CGContextAddPath
我认为这个问题可能与 renderInContextView
没有考虑我的绘图有关,或者我没有正确地将路径添加到上下文?
放大代码基于GJNeilson's work ,我所做的就是将放大镜的中心点固定在左上角并取下 mask 。
最佳答案
我认为您是在画线,然后在其上绘制图像。最后尝试调用 drawLine
。
此外,当您绘制可能将其定位在屏幕外的线时,缩放和平移仍然处于事件状态。您可能必须使用 CGContextSaveGState
和 CGContextRestoreGState
关于ios - 贝塞尔曲线路径不在上下文中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42146398/