如何在swift 3中的图像上的两点之间画一条线?

如何在swift 3中的图像上的两点之间画一条线?,第1张

概述我是 swift的新手,我想在我称为mapView的图像上画两条点之间的线,我试图使用CGContext但没有结果,任何想法都有帮助吗?谢谢. UIGraphicsBeginImageContext(mapView.bounds.size) let context : CGContext = UIGraphicsGetCurrentContext()! context.addLin 我是 swift的新手,我想在我称为mapVIEw的图像上画两条点之间的线,我试图使用CGContext但没有结果,任何想法都有帮助吗?谢谢.

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()
解决方法 一种选择是在图像视图中添加子视图,并将线条绘制代码添加到其绘图(_ rect:CGRect)方法中.

示例游乐场实施:

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 imagelet lineVIEw = lineVIEw(frame: imageVIEw.frame)imageVIEw.addSubvIEw(lineVIEw)
总结

以上是内存溢出为你收集整理的如何在swift 3中的图像上的两点之间画一条线?全部内容,希望文章能够帮你解决如何在swift 3中的图像上的两点之间画一条线?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1006228.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存