【发布时间】:2018-11-14 06:46:59
【问题描述】:
我正在尝试在 UIImage 上绘制矩形(CGRects 数组),并将其呈现给最终用户。 最后的结果,因为没有更好的短语,看起来很奇怪。例如,如果我有一个包含 2 个 CGRect 的数组,则仅绘制一个,勉强完成,但不完全完成。另一个被省略。请参阅下图,了解图像如何使用特定的 CGRect 数组生成。 (仅供参考:我将图像发送到 Google Cloud 进行处理,它以 CGRects 数组响应)
我错过了什么?
private func drawOccurrencesOnImage(_ occurrences: [CGRect], _ image: UIImage) -> UIImage? {
let imageSize = image.size
let scale: CGFloat = 0.0
UIGraphicsBeginImageContextWithOptions(imageSize, false, scale)
image.draw(at: CGPoint.zero)
let ctx = UIGraphicsGetCurrentContext()
ctx?.addRects(occurrences)
ctx?.setStrokeColor(UIColor.red.cgColor)
ctx?.setLineWidth(2.0)
ctx?.strokePath()
guard let drawnImage = UIGraphicsGetImageFromCurrentImageContext() else {
presentAlert(alertTitle: "Error", alertText: "There was an issue, please try again")
return nil
}
UIGraphicsEndImageContext()
return drawnImage
}
值为“[(298.0, 868.0, 65.0, 43.0), (464.0, 1017.0, 67.0, 36.0)]" 的 CGRect 数组(上述函数中的“出现次数”)会产生下图:
更多细节:使用 Swift 4、iOS 12
谢谢!
【问题讨论】:
-
我完全使用了您的代码,但使用了其他矩形。它工作得很好。看来,您的矩形中的 x 和 y 太大了。你能仔细检查一下吗?也许你需要以某种方式调整矩形。
-
嗨@Evgeniy - 感谢您的评论!你能提供你在完美工作的地方使用的其他矩形吗?会超级有帮助的。 & 你在什么手机上测试?非常感谢。
-
我在 XS 模拟器上进行测试。我测试了 CGRect(x: 10, y: 10, width: 100, height: 100) 和 CGRect(x: 150, y: 150, width: 100, height: 100)
-
太好了 - 谢谢。
标签: ios swift uiimage drawrect cgrect