我是 AVFoundation 的新手,正在尝试实现摄像机。基本上,当您单击按钮时,它会调用
showCamera
方法。它将创建会话,添加音频和视频输入,并添加视频输出。
如何添加
AVCaptureConnection
?
- (IBAction) showCamera
{
//Add the camview to the current view ontop of controller
[[[[[UIApplication sharedApplication] delegate] self] window] addSubview:camView];
session = [[AVCaptureSession alloc] init];
//Set preset on session to make recording scale high
if ([session canSetSessionPreset:AVCaptureSessionPresetHigh]) {
session.sessionPreset = AVCaptureSessionPresetHigh;
}
// Add inputs and outputs.
NSArray *devices = [AVCaptureDevice devices];
//Print out all devices on phone
for (AVCaptureDevice *device in devices)
{
if ([device hasMediaType:AVMediaTypeVideo])
{
if ([device position] == AVCaptureDevicePositionBack)
{
//Add Rear Video input to session
[self addRearCameraInputToSession:session withDevice:device];
}
}
else if ([device hasMediaType:AVMediaTypeAudio])
{
//Add Microphone input to session
[self addMicrophoneInputToSession:session withDevice:device];
}
else
{
//Show error that your camera does not have a phone
}
}
//Add movie output
[self addMovieOutputToSession:session];
//Construct preview layer
[self constructPreviewLayerWithSession:session onView:camView];
}
您无需手动添加 AVCaptureConnections。当您将输入和输出添加到 AVCaptureSession 对象时,系统会自动为您创建连接。引用文档:
将输入或输出添加到会话时,会话会贪婪地在所有兼容的捕获输入端口和捕获输出之间形成连接。
除非您需要禁用自动创建的连接之一,或更改
videoMirrored
或 videoOrientation
属性,否则您根本不必担心它们。