ios - 贝塞尔曲线路径不在上下文中绘制

标签 ios swift

我正在尝试为我的用户提供他们在 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

此外,当您绘制可能将其定位在屏幕外的线时,缩放和平移仍然处于事件状态。您可能必须使用 CGContextSaveGStateCGContextRestoreGState

重置它

关于ios - 贝塞尔曲线路径不在上下文中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42146398/

相关文章:

ios - 带有声音/音频的 NSMutableAttributedString

swift - Google API 自动完成

ios - Swift 3 错误处理和变量可访问性

ios - 排除 UIKit 显示问题

ios - swift中错误和异常的区别

ios - "Cannot add output"当我尝试在 AVPlayer 中进入背景时崩溃

ios - 计算 CMMotionActivity 间隔

ios - Try-Catch 错误 Objective-C

ios - 无法添加一致的高度和填充。动态表格 View 单元格之间

ios - 如何从以编程方式添加的 UITextView 获取文本 [Swift]