iosobjective-cavfoundationavcam

Why doesn't the AVCam sample code use layerWithSession?


I'm trying to puzzle out the Objective-C AVCam sample code from Apple but I don't see exactly where it adds the capture session to the preview layer. Our code creates an AVCaptureVideoPreviewLayer pointer using the layerWithSession interface, but this causes a problem if we use it on the main thread, as it can take upwards of 10 seconds to finish. And what little other sample code I find on the web uses the same interface.

But I don't see that anywhere in the ObjC code for the AVCam sample. It assigns a session to the AVCamPreviewView, but as far as I can tell, that merely stores the object. Or does that in fact assign the session to the UIView?


Solution

  • It is the following line that ensures the PreviewView has a AVCaptureVideoPreviewLayer.

    + (Class)layerClass
    {
        return [AVCaptureVideoPreviewLayer class];
    }
    

    layerClass() is called internally to find out what layer should be attached to this UIView when it is initialised. setSession() is then setting the session for this preview layer.