2016-12-14 5 views
2

신속한 작업이 새로운데, mapView라는 이미지 위에 2 포인트 사이의 선을 그려야합니다. CGContext를 사용하려고했지만 아무런 결과가 없습니까? 감사.신속하게 이미지 위에 두 점 사이에 선을 그리는 방법 3?

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() 
+0

당신은 당신이 구글 맵을 사용하고 폴리 라인을 그릴 원하거나 당신이 두 점 사이에 선을 그릴하려는, 폴리 라인 –

+0

을 찾고 수 있습니다 어떤 UIImage. –

답변

3

하나의 옵션은 이미지보기로 하위 뷰를 추가하고 해당 draw(_ rect: CGRect) 방법으로 선 그리기 코드를 추가하는 것입니다.

샘플 놀이터 구현 : @Dev_Tandel 말했듯이

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) 
+0

도움을 주셔서 감사합니다. 제발 다른 질문하시기 바랍니다 : 어떻게 이미지에서 이러한 라인을 삭제할 수 있습니까? removeFromSuperView 시도했지만 이미지를 삭제하려면 싶지 않아 함께 다른보기가있다'lineView.removeFromSuperview()'사용할 수있는 덕분에 –

+0

, 다른 옵션은 LineView의 그리기 기능을 제어하는 ​​것입니다 무엇을 표시하려면 네가 원해. –

+0

대단히 고맙습니다. 포인트를 보내서 동적으로 만들려고했으나 동적으로 만드는 방법을 알려주지 못했습니다. –