How to convert VNRectangleObservation item to UIImage in SwiftUI

I was able to identify squares from a images using VNDetectRectanglesRequest. Now I want those rectangles to store as separate images (UIImage or cgImage). Below is what I tried.

        let rectanglesDetection = VNDetectRectanglesRequest { request, error in
            rectangles = request.results as! [VNRectangleObservation]
            rectangles.sort{$0.boundingBox.origin.y > $1.boundingBox.origin.y}
            
            for rectangle in rectangles {

                let rect = rectangle.boundingBox
                let imageRef = cgImage.cropping(to: rect)
                let image = UIImage(cgImage: imageRef!, scale: image!.scale, orientation: image!.imageOrientation)

                checkBoxImages.append(image)
        }

Can anybody point out what's wrong or what should be the best approach?

x and y coordinates in observation are in UnitPoints of I remember correctly. They go from 0 to 1 instead of 0 to width/height. You would have to multiply them by the width or height of the picture.

How to convert VNRectangleObservation item to UIImage in SwiftUI
 
 
Q