Reckoner
Reckoner

Reputation: 1041

AVFoundation camera not visible

I have just started AVFoundation and I'm trying to capture an image.The problem is that I'm able to capture the image but unable to see the camera in my view.Here is my code:

     session = [AVCaptureSession new];
if([session canSetSessionPreset:AVCaptureSessionPresetHigh])
 [session setSessionPreset:AVCaptureSessionPresetHigh];//specifies photo quality

AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
[session addInput:deviceInput];

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view]layer];
[rootLayer setMasksToBounds:YES];
[previewLayer setFrame:_cameraView.frame];
[rootLayer insertSublayer:previewLayer atIndex:0];

stillImageOutput = [AVCaptureStillImageOutput new];
[stillImageOutput setOutputSettings:[[NSDictionary alloc]initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]];
[session addOutput:stillImageOutput];
    [session startRunning];

Is there anything wrong with my code?

Upvotes: 0

Views: 260

Answers (1)

David P
David P

Reputation: 1043

Try [[rootLayer layer] addSublayer:previewLayer];

For me it works!

Upvotes: 1

Related Questions