2

I have been working on being able to draw a red line on an image view using swift 3.0 and have ran across some problems. As you could guess, I am using some code from stack overflow to help me with my first initial attempt at this and am getting a weird bug.

As I drag my finger across the imageview it does draw a red line but for each "frame" it drops all that I drew down further on the screen. So as I draw it looks like the lines I am drawing are falling and then just completely go off screen.

What I am looking for is to be able to simply draw on the image view and instead of everything moving down each frame, I would like it to keep in the same position similar to how it works on all the drawing apps and etc.

Below is all the code:

@IBOutlet weak var mainImageView: UIImageView!
var lastPoint = CGPoint.zero
var fromPoint = CGPoint();
var toPoint = CGPoint.zero
var red: CGFloat = 255.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var brushWidth: CGFloat = 10.0
var opacity: CGFloat = 1.0
var swiped = false

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint)
{
    UIGraphicsBeginImageContextWithOptions(self.mainImageView.bounds.size, false, 0);
    mainImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
    let context = UIGraphicsGetCurrentContext()

    context?.move(to: fromPoint)
    context?.addLine(to: toPoint)
    context?.setLineCap(CGLineCap.round)
    context?.setLineWidth(brushWidth)
    context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
    context?.setBlendMode(CGBlendMode.normal)
    context?.strokePath()

    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    mainImageView.alpha = opacity
    UIGraphicsEndImageContext()

}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = false;
    if let touch = touches.first {
        lastPoint = touch.location(in: self.mainImageView)
    }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = true;
    if let touch = touches.first {
        let currentPoint = touch.location(in: mainImageView)
        drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
        lastPoint = currentPoint
    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if(!swiped){
        self.drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
    }
}

Thanks for the help guys!

Bryce
  • 447
  • 2
  • 9
  • 24

3 Answers3

2

Following code you can Draw the image using touch

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {


if let touch = touches.first as? UITouch{

    prevPoint1 = touch.previousLocationInView(self.view)

    prevPoint2 = touch.previousLocationInView(self.view)

    lastPoint = touch.locationInView(self.view)

}

}



 override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {


if let touch = touches.first as? UITouch{

    let currentPoint = touch.locationInView(view)



    prevPoint2 = prevPoint1

    prevPoint1 = touch.previousLocationInView(self.view)





    UIGraphicsBeginImageContext(view.frame.size)

    let context = UIGraphicsGetCurrentContext()

    TempImage.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))



    var mid1 = CGPointMake((prevPoint1.x + prevPoint2.x)*0.5, (prevPoint1.y + prevPoint2.y)*0.5)

    var mid2 = CGPointMake((currentPoint.x + prevPoint1.x)*0.5, (currentPoint.y + prevPoint1.y)*0.5)



    CGContextMoveToPoint(context, mid1.x, mid1.y)

    CGContextAddQuadCurveToPoint(context, prevPoint1.x, prevPoint1.y, mid2.x, mid2.y)

    //CGContextAddLineToPoint(context, toPoint.x, toPoint.y)



    CGContextSetLineCap(context, kCGLineCapRound)

    CGContextSetLineWidth(context, brushWidth)

    CGContextSetRGBStrokeColor(context, red, green,blue, 1.0)

    CGContextSetBlendMode(context, kCGBlendModeNormal)



    CGContextStrokePath(context)



    TempImage.image = UIGraphicsGetImageFromCurrentImageContext()

    TempImage.alpha = opacity

    UIGraphicsEndImageContext()



    lastPoint = currentPoint

}

 }



 override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {



UIGraphicsBeginImageContext(MainImage.frame.size)

MainImage.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)

TempImage.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: opacity)

MainImage.image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()



TempImage.image = nil

}
Vignesh J
  • 257
  • 1
  • 2
  • 14
2

Following code you can Draw the image using touch (Swift 3.0)

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
swiped = false
if let touch = touches.first as? UITouch {
    lastPoint = touch.location(in: self.view)
}
 }
 func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

UIGraphicsBeginImageContext(view.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width:     view.frame.size.width, height: view.frame.size.height))


context?.move(to: fromPoint)
context?.addLine(to: toPoint)

context?.setLineCap(CGLineCap.round)
context?.setLineWidth(brushWidth)
context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
context?.setBlendMode(CGBlendMode.normal)
context?.strokePath()


tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
tempImageView.alpha = opacity
UIGraphicsEndImageContext()

}

 func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
swiped = true
if let touch = touches.first as? UITouch {
    let currentPoint = touch.location(in: view)
    drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)

    lastPoint = currentPoint
}
}
Vignesh J
  • 257
  • 1
  • 2
  • 14
0

Solution for Swift 5.7.2


import UIKit

class DrawerViewController: UIViewController {
    
    enum Spec {
        static let brushColor = UIColor.green
        static var brushWidth: CGFloat = 10.0
        static var opacity: CGFloat = 1.0
    }
    
    var lastPoint = CGPoint.zero
    var swiped = false
        
    private var maskView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // MaskView
        view.addSubview(maskView)
        maskView.translatesAutoresizingMaskIntoConstraints = false
        maskView.backgroundColor = .white
        NSLayoutConstraint.activate([
            maskView.topAnchor.constraint(equalTo: view.topAnchor),
            maskView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            maskView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            maskView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        swiped = false
        if let touch = touches.first {
            lastPoint = touch.location(in: self.view)
        }
    }
    
    func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

        UIGraphicsBeginImageContext(view.frame.size)
        let context = UIGraphicsGetCurrentContext()
        maskView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))

        context?.move(to: fromPoint)
        context?.addLine(to: toPoint)

        context?.setLineCap(CGLineCap.round)
        context?.setLineWidth(Spec.brushWidth)
        context?.setStrokeColor(Spec.brushColor.cgColor)
        context?.setBlendMode(CGBlendMode.normal)
        context?.strokePath()


        maskView.image = UIGraphicsGetImageFromCurrentImageContext()
        maskView.alpha = Spec.opacity
        UIGraphicsEndImageContext()
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        swiped = true
        if let touch = touches.first {
            let currentPoint = touch.location(in: view)
            drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)

            lastPoint = currentPoint
        }
    }
}
Tony Macaren
  • 437
  • 5
  • 11