Visionkit can lift a subject. But the bounding rectangle is always returning x,y,width,height values as 0,0,0,0

In our app, we needed to use visionkit framework to lift up the subject from an image and crop it. Here is the piece of code:

if #available(iOS 17.0, *) {
            let analyzer = ImageAnalyzer()
            let analysis = try? await analyzer.analyze(image, configuration: self.visionKitConfiguration)
            let interaction = ImageAnalysisInteraction()
            interaction.analysis = analysis
            interaction.preferredInteractionTypes = [.automatic]
            guard let subject = await interaction.subjects.first else{
                return image
            }
            let s = await interaction.subjects
            print(s.first?.bounds)
            guard let cropped = try? await subject.image else { return image }
            return cropped
        }

But the s.first?.bounds always returns a cgrect with all 0 values. Is there any other way to get the position of the cropped subject? I need the position in the image from where the subject was cropped. Can anyone help?

This happens if the interaction doesn't have an associated view. I think this should probably emit some kind of warning, but instead it just returns (0,0,0,0) as the bounds.

To fix it, you need someImageView.addInteraction(interaction) before you access the subjects array.

Visionkit can lift a subject. But the bounding rectangle is always returning x,y,width,height values as 0,0,0,0
 
 
Q