AVCaptureVideoPreviewLayer (camera preview) freezes/stuck after moving to background and back

For future readers: this is the correct process to setting up camera inside your app.

First of all, thanks for the folks above that took their time and tried help me. They both direct me into the correct direction. Although Bill was mistaken about the viewDidLoad theory, he did gave the solution Apple Project.

This setting up camera - the correct way - is little more complicated than I thought, follow the documentation gave me excellent results. So for the Objective-C coders:

Objective C cam project

Swift cam project

About Andrea answer, he did said some great pointers you should consider when you are creating this kind of app. Check them out - they are highly relevant (most of the things he said inside the Apple project as well).


Roi,

I think your problem is that you are doing all the session setup and such in the viewWillAppear. Lets say that the captureSession, and the previewLayer were both alloc'd and working correctly. Now, you put app into the background and bring back.

You will immediately try to create a new captureSession, and a new previewLayer. I suspect that the old ones and the new ones are getting tangled up.

In the Apple AVCam example they do the setup in the viewDidLoad. That way it is only done once.

You should move all your setup stuff to a method and then call the method from the viewDidLoad.

bill


Just a quick update In 2017 if someone suffering overthinking such that thing,

Do the same thing but change your

stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

replace it with

stillImageOutput!.outputSettings = [((kCVPixelBufferPixelFormatTypeKey as NSString) as String):NSNumber(value:kCVPixelFormatType_32BGRA)]

it will solve the issue. If not, then come back here and write down :))