2

ImageView is set to "Scale to Fill". When trying to draw the bounding box on the image with no transformations, the box appears pixels above where it should, though correctly aligned on the x axis. Example image: https://i.stack.imgur.com/bFIDP.jpg

//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!
}

When a try the below transformation solution that I've seen on a few threads the Rect disappears entirely.

 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)
Jack Gruber
  • 163
  • 1
  • 14

0 Answers0