How to draw a line between two points over an image in swift 3?

mohammed HASSAN

I am new to swift, I want to draw a line between 2 points over an image which I called mapView, I tried to use CGContext but got no result, any idea to help? Thanks.

UIGraphicsBeginImageContext(mapView.bounds.size)
    let context : CGContext = UIGraphicsGetCurrentContext()!
    context.addLines(between: [CGPoint(x:oldX,y:oldY), CGPoint(x:newX, y:newY)])
    context.setStrokeColorSpace(CGColorSpaceCreateDeviceRGB())
    context.setStrokeColor(UIColor.blue.cgColor.components!)
    context.setLineWidth(3)
    mapView?.image?.draw(at: CGPoint(x:0, y:0))
    context.strokePath()
    mapView.image = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
Jozef Legény

One option is to add a sub view to your image view and add the line drawing code into its draw(_ rect: CGRect) method.

A sample playground implementation:

class LineView : UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(white: 0.0, alpha: 0.0)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext() {
            context.setStrokeColor(UIColor.blue.cgColor)
            context.setLineWidth(3)
            context.beginPath()
            context.move(to: CGPoint(x: 5.0, y: 5.0)) // This would be oldX, oldY
            context.addLine(to: CGPoint(x: 50.0, y: 50.0)) // This would be newX, newY
            context.strokePath()
        }
    }
}


let imageView = UIImageView(image: #imageLiteral(resourceName: "image.png")) // This would be your mapView, here I am just using a random image
let lineView = LineView(frame: imageView.frame)
imageView.addSubview(lineView)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to draw a line between two views in Swift 3?

How to draw a line / link between two points on a D3 map based on latitude / longitude?

Draw arrow between on line between two points

Matplotlib how to draw vertical line between two Y points

How to draw a line between two given points in R with plotly?

How to draw a line between two points objective-C

How to draw a line between two points with js and CSS

Draw line between two distinct points

R: draw a line between two points in ggplot

Draw line between two points in JavaScript?

Draw curved line between two points

How to draw image dynamically on a canvas line between two cells

Real distance between two points over image

Draw a line between points

How to draw a line over two graphs?

How to draw a line in Swift 3

Animate a line between two points - swift

How to draw a line between two points when holding one axis fixed (time series)

How to draw line between two points in JavaScript - Google Maps Api Route

Javascript + svg, draw sinus (wave) line between two given points

Draw line between two given points (OpenCV, Python)

ChartJS: Draw line between two data points on hover

Is there a way to draw a line between two points on a HTML page without Canvas?

How can I maintain the same distance between two points over the outer line of the semi-circle?

How do I draw an arrow between two points in d3v4?

How to find some additional points on a line between two points in 3D?

Draw an arc between two points

Draw a line between two pixels on a grayscale image in Julia

How can draw a line using the x and y coordinates of two points?