3

I am working with Apple's Vision framework for detecting faces. Then I come to this function.

let cgr: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)

let videoPreviewRect = previewLayer.layerRectConverted(fromMetadataOutputRect: cgr)

where previewLayer is AVCaptureVideoPreviewLayer type object. It is a conversion from metaDataOutput rect to view rect. But I don't understand it completely.

How does it work, what does it converts to what?

halfer
  • 19,824
  • 17
  • 99
  • 186
Asif Mujtaba
  • 447
  • 6
  • 17

1 Answers1

0

So the idea here is that typically with something like Vision Framework or AVCaptureMetadataOutput you will get back normalised rectangle coordinates (e.g {0,0}-{1,1} range, think like a percentage of the total image). However, if you want to draw them to the user you will likely going to need the coordinates in screen points.

The pair of methods of layerRectConverted(fromMetadataOutputRect:) and metadataOutputRectConverted(fromLayerRect:) will allow you to convert between these two coordinate systems and also takes into account videoGravity property of AVCaptureVideoPreviewLayer which otherwise makes it a pain to calculate things e.g. when resizeAspectFill gravity is used.

zaitsman
  • 8,984
  • 6
  • 47
  • 79