如何使用AVFoundation录制视频

时间:2015-08-07 12:12:27

标签: iphone ios7 ios8 avfoundation

需要使用AVFoundation录制视频。谷歌搜索了很多但没有成功。显示我使用的下面的代码,它打开相机,但没有得到如何开始录制和保存。

- (void)viewDidLoad
{
[super viewDidLoad];
[self setupCameraSession];

}

- (void)setupCameraSession
{    
ICLog;

// Session
AVCaptureSession *session = [AVCaptureSession new];    
[session setSessionPreset:AVCaptureSessionPreset1920x1080];

// Capture device
AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;

// Device input
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
if ( [session canAddInput:deviceInput] )
    [session addInput:deviceInput];

// Preview
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];    
[previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
[previewLayer setFrame:CGRectMake(-70, 0, rootLayer.bounds.size.height, rootLayer.bounds.size.height)];
[rootLayer insertSublayer:previewLayer atIndex:0];

[session startRunning];
} 

请指导以上。

1 个答案:

答案 0 :(得分:-1)

要使用AVFoundation录制视频,您可以使用以下代码:

-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller
usingDelegate:(id )delegate {
// 1 - Validattions
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
    || (delegate == nil)
    || (controller == nil)) {
    return NO;
}
// 2 - Get image picker
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
// Displays a control that allows the user to choose movie capture
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
// 3 - Display image picker
[controller presentModalViewController: cameraUI animated: YES];
return YES;

}

有关详细说明,您可以查看此Apple Documentation。使用AVFoundation录制视频的一些非常有用的教程是: