IOS AVCaptureVideoPreviewLayer not showing in UIView?

Problem

I have an example application from Apple (SquareCam) , which I use as a link to create a user interface for the camera. I am using the AVFoundation framework. If I create and run a project from Apple, the application works as expected. However, if I take the same code from this project and put it in a new project in Xcode, the video preview will not be displayed.

I simplified the code to the main components in order to run the video preview layer. Again, this code works fine, as in the Apple project (SquareCam), but does not appear in my new project.

The code

- (void)setupAVCapture { NSError *error = nil; AVCaptureSession *session = [AVCaptureSession new]; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) [session setSessionPreset:AVCaptureSessionPreset640x480]; else [session setSessionPreset:AVCaptureSessionPresetPhoto]; // Select a video device, make an input AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if ([session canAddInput:deviceInput]) [session addInput:deviceInput]; previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; [previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect]; CALayer *rootLayer = [previewView layer]; [rootLayer setMasksToBounds:YES]; [previewLayer setFrame:[rootLayer bounds]]; [rootLayer addSublayer:previewLayer]; [session startRunning]; } - (void)viewDidLoad { [super viewDidLoad]; [self setupAVCapture]; } 

What have i tried?

I have all the correct settings. I have all the framework libraries in my build phase. Both projects use storyboards. What is strange is that I can capture the image from the camera and even switch the source from front to back. But the preview will not be displayed. Apple project has not been configured using ARC. So, I updated the project to use ARC. Again, it works great in an Apple project, but not in a new project.

Thoughts?

Any ideas on what could be the problem? Could this be the reason for this project?

+8
ios objective-c xcode video avfoundation
source share
2 answers

Had the same problem and the key set the AVCaptureVideoPreviewLayer frame to the borders of the UIView:

 // Setup camera preview image AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession]; previewLayer.frame = _cameraPreview.bounds; [_cameraPreview.layer addSublayer:previewLayer]; 
+17
source share

I had a similar problem in my project. From your code snippet, it's hard to tell who is calling the setupAVCapture method. In my case, I had to make sure that AVCaptureVideoPreviewLayer was created in the main thread.

There are several options. You can call setupAVCapture in the main application thread by wrapping the call in the following block:

 dispatch_async(dispatch_get_main_queue(), ^{ // call setupAVCapture() method in here }); 

If this is not an option, you can only do this in the following part of your code:

  dispatch_async(dispatch_get_main_queue(), ^{ previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; [previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect]; CALayer *rootLayer = [previewView layer]; [rootLayer setMasksToBounds:YES]; [previewLayer setFrame:[rootLayer bounds]]; [rootLayer addSublayer:previewLayer]; }); 
+4
source share

All Articles