无法使用AVCaptureSession获取任何视频预览

时间:2011-10-26 11:56:03

标签: ios video ios5 avfoundation avcapturesession

我设置AVCaptureSession就像这样:

AVCaptureSession *newSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;

AVCaptureDeviceInput *newInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];


if(error) {
    NSLog([error localizedDescription]);

}

AVCaptureMovieFileOutput *newOutput = [[AVCaptureMovieFileOutput alloc] init];


if([newSession canAddInput:newInput])
    [newSession addInput:newInput];
if ([newSession canAddOutput:newOutput])
    [newSession addOutput:newOutput];

[self setSession:newSession];
[self setInput:newInput];
[self setOutput:newOutput];

然后我在我的视图中添加了一个预览图层:

AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[recorder session]];

[layer setBounds:[recordingView bounds]];
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

[[recordingView layer] insertSublayer:layer above:[recordingView layer]];

但我不会预览。

我正在IOS 5上使用iPod Touch第4代。

感谢。

1 个答案:

答案 0 :(得分:2)

首先,您的recordingView是如何定义的? 第二,

[[recordingView layer] insertSublayer:layer above:[recordingView layer]];

毫无意义。 (您试图在自己的图层中添加自己图层上方的图层 - > duh?) 请改为[recordingView.layer addSublayer:layer];

您在评论中发布的项目使用归零弱指针。标记你的属性强大,一切正常。

相关问题