0

I am able to draw a rectangle using the code below (works). However, I am using the Vison framework to detect rectangles but it is giving me back CGPoint values that is less than 1.0. When I enter these coordinates to draw a rectangle I get nothing back. Please can some advise?

Works - i get a rectangle

    let rectangle = UIBezierPath.init()

    rectangle.move(to: CGPoint.init(x: 100, y: 100))
    rectangle.addLine(to: CGPoint.init(x: 200, y: 130))
    rectangle.addLine(to: CGPoint.init(x: 300, y: 400))
    rectangle.addLine(to: CGPoint.init(x: 100, y: 500))

    rectangle.close()

    let rec = CAShapeLayer.init()
    rec.path = rectangle.cgPath
    rec.fillColor = UIColor.red.cgColor
    self.view.layer.addSublayer(rec)

Does not work (no rectangle):

    let rectangle = UIBezierPath.init()

    rectangle.move(to: CGPoint.init(x: 0.154599294066429, y: 0.904223263263702))

    rectangle.addLine(to: CGPoint.init(x: 0.8810795545578, y: 0.970198452472687))
    rectangle.addLine(to: CGPoint.init(x: 0.16680309176445, y: 0.0157230049371719))
    rectangle.addLine(to: CGPoint.init(x: 0.878569722175598, y: 0.128135353326797))

    rectangle.close()

    let rec = CAShapeLayer.init()
    rec.path = rectangle.cgPath
    rec.fillColor = UIColor.red.cgColor
    self.view.layer.addSublayer(rec)
4

2 回答 2

1

Vision 框架返回的点是由视口全尺寸百分比表示的坐标。如果您的视口是 640 x 480(例如),那么您的第一点是CGPoint(x: 0.154599294066429 * 640, y: 0.904223263263702 * 480). 考虑到这一点,将您的代码更改为这样的,应该没问题:

let rectangle = UIBezierPath.init()
let width = // your width - Maybe UIScreen.main.bounds.size.width ?
let height = // your height - Maybe UIScreen.main.bounds.size.height ?

rectangle.move(to: CGPoint.init(x: width  * 0.154599294066429, y: height * 0.904223263263702))

rectangle.addLine(to: CGPoint.init(x: width * 0.8810795545578, y: height * 0.970198452472687))
rectangle.addLine(to: CGPoint.init(x: width * 0.16680309176445, y: height * 0.0157230049371719))
rectangle.addLine(to: CGPoint.init(x: width * 0.878569722175598, y: height * 0.128135353326797))

rectangle.close()

let rec = CAShapeLayer.init()
rec.path = rectangle.cgPath
rec.fillColor = UIColor.red.cgColor
self.view.layer.addSublayer(rec)
于 2018-05-15T08:04:01.530 回答
1

让我们分析一下你在这里得到了什么。在第一个示例中,您正在绘制一个大小约为 200 像素 x 400 像素的矩形,嗯……当然,这将完全按照您的预期显示。

在第二个示例中,您正在绘制一个大约 0.7 像素 x 0.8 像素的矩形。现在从逻辑上思考这个问题,屏幕应该如何表示 0.7 个像素?不能!像素是您可以拥有的最小表示形式,即单个彩色方块。

由于屏幕/系统的物理限制,该代码不起作用。您需要使用大于 1 的大小(和位置)值才能看到矩形。Vision 框架没有什么不同,它只会看到你能看到的,如果这有意义的话。

于 2018-05-15T07:57:46.983 回答