Bezier Path Not Drawing on context

ThrowingSpoon

I'm trying give my user fine selection of a point they touch on a UIImage. I have a magnifying square in the top left corner that shows where they're touching at 2x zoom. It works well.

I'm trying to add a "crosshair" in the center of the magnifying area to make selection clearer.

With the code below no line is visible.

//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()
}

It's possible that the line is being drawn but too small or not where I expect it to be.

I've tried other ways of drawing the line like using CGContextAddPath

I think the issue might be related to the renderInContextView not taking my drawing into account, or I'm not adding the path to the context correctly?

The magnification code is based on GJNeilson's work, all I've done is pin the centre point of the magnifying glass to the top left and remove the mask.

Craig Siemens

I think you're drawing the line then drawing the image over it. Try calling drawLine last.

Also, the scale and translate are still active when you draw the line which may be positioning it offscreen. You might have to reset it using CGContextSaveGState and CGContextRestoreGState

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related