2015-10-14 30 views
0

我刚刚开始AVFoundation,我试图捕捉图像。问题是我能够捕捉图像,但无法在我的视图中看到相机。以下是我的代码:AVFoundation相机不可见

 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

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

对我来说,它的工作原理!

+0

是的,我已经这样做了!!!还是谢谢一个哥们........... – Reckoner