Skip to content

Instantly share code, notes, and snippets.

@cameronehrlich
Last active February 20, 2017 07:24
Show Gist options
  • Select an option

  • Save cameronehrlich/986c96fe35cc7f70aac2 to your computer and use it in GitHub Desktop.

Select an option

Save cameronehrlich/986c96fe35cc7f70aac2 to your computer and use it in GitHub Desktop.
Gives an example of how to initialize an video capture session
// MEModel.h
@interface MEModel : NSObject
@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureDevice *frontCamera;
@property (nonatomic, strong) AVCaptureDevice *backCamera;
@property (nonatomic, strong) AVCaptureDeviceInput *inputDevice;
@property (nonatomic, strong) AVCaptureMovieFileOutput *videoFileOutput;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer; // Once the session is setup, this can just easily be added to any view. i.e. [self.view.layer addSublayer:[[MEModel sharedInstance] previewLayer];
@end
// MEModel.m
#import "MEModel.h"
@implementation MEModel
+ (instancetype)sharedInstance
{
static MEModel *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[MEModel alloc] init];
});
return instance;
}
- (instancetype)init
{
self = [super init];
if (self) {
[self initializeCaptureSession];
}
return self;
}
#pragma mark -
#pragma mark AVFoundation Setup
- (void)initializeCaptureSession
{
self.session = [[AVCaptureSession alloc] init];
[self initializeCameraReferences];
// Video
self.videoFileOutput = [[AVCaptureMovieFileOutput alloc] init];
[self.session addOutput:self.videoFileOutput];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.session startRunning];
[self beginRecordingWithDevice:self.frontCamera];
}
- (void)initializeCameraReferences
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
{
if (device.position == AVCaptureDevicePositionBack) {
self.backCamera = device;
}
else if (device.position == AVCaptureDevicePositionFront) {
self.frontCamera = device;
}
}
}
- (void)beginRecordingWithDevice:(AVCaptureDevice *)device
{
[self.session stopRunning];
if (self.inputDevice) {
[self.session removeInput:self.inputDevice];
}
NSError *error = nil;
self.inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (error) {
NSLog(@"Error: %@", error.debugDescription);
return;
}
[self.session addInput:self.inputDevice];
self.session.sessionPreset = AVCaptureSessionPreset640x480; // Replace with whichever preset you want
[self.session startRunning];
}
- (void)toggleCameras
{
BOOL isBackFacing = (self.inputDevice.device == self.backCamera);
[self.session stopRunning];
if (isBackFacing) {
[self beginRecordingWithDevice:self.frontCamera];
}
else {
[self beginRecordingWithDevice:self.backCamera];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment