I am creating an application for the iPad, and one of its functions is scanning QR codes. I have part of the QR scan, but the problem is that the iPad screen is very large and I will scan small QR codes of a sheet of paper with many QR codes visible immediately. I want to designate a smaller display area to be the only area that can actually capture a QR code, so it’s easier for the user to scan the specific QR code that they need.
Currently, I have made a temporary UIView with red borders that are centered on the page as an example of where I want the user to scan QR codes. It looks like this:

I went through everything to find an answer to how I can target a specific AVCaptureVideoPreviewLayer region to collect QR code data, and what I found are suggestions for using "rectOfInterest" with AVCaptureMetadataOutput. I tried to do this, but when I set rectOfInterest to the same coordinates and size as the ones I use for my UIView, which displays correctly, I can no longer scan and recognize any QR codes. Can someone please tell me why the scan area does not match the location of the UIView that is visible, and how can I get rectOfInterest to be in the red borders that I added to the screen?
Here is the code for the scan function that I am currently using:
func startScan() {
Update The reason I don't think this is a duplicate is because the other link specified in Objective-C and my code is in Swift. For those of us new to iOS, it's not easy to translate these two. In addition, the response link does not show the actual update made in the code that resolved its problem. He left a good explanation of the need to use the metadataOutputRectOfInterestForRect method to convert the coordinates of the rectangle, but I still can’t get this method to work, because I don’t understand how this should work without an example.
source share