1

ImageView 设置为“缩放填充”。当试图在没有转换的情况下在图像上绘制边界框时,框会出现在它应该在上方的像素,尽管在 x 轴上正确对齐。示例图片:https ://imgur.com/a/Ef4UPbd

//The Settings for the VNImageRectForNormalizedRect being passed to draw on image.
VNImageRectForNormalizedRect(boundingBox, sampleImage.width, sampleImage.height)
 }
    func drawOnImage(_ image: UIImage, rects: [CGRect]) -> UIImage {
         
         // Create a context of the starting image size and set it as the current one
        UIGraphicsBeginImageContext(image.size)
        image.draw(at: CGPoint.zero)
        // Get the current context
        let context = UIGraphicsGetCurrentContext()!
        for rect in rects {
        //draw rect
        context.setLineWidth(15.0)
        context.setStrokeColor(UIColor.red.cgColor)
        context.addRect(rect)
        context.strokePath()
        }
 let finalImage = UIGraphicsGetImageFromCurrentImageContext()
         UIGraphicsEndImageContext()
         // Return modified image
        imageView.image = finalImage
        return finalImage!
}

当尝试以下我在几个线程上看到的转换解决方案时,Rect 完全消失了。

 let transform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -imageView.frame.size.height)

 let scale = CGAffineTransform.identity.scaledBy(x: imageView.frame.width, y: imageView.frame.size.height)
 let transformedRect = rect.applying(scale).applying(transform)
4

0 回答 0