AVFoundation相机不可见

时间:2015-10-14 12:01:14

标签: ios avfoundation

我刚刚开始使用AVFoundation而我正在尝试拍摄图像。问题是我能够捕获图像但是无法在我的视图中看到相机。这是我的代码:< / p>

     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];

我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:1)

尝试[[rootLayer layer] addSublayer:previewLayer];

对我而言,它有效!