Создаем видео сессию, становимся делегатом: и потом получаем картинку из семпла. С этой картинкой потом можете работать как вам угодно.
- (void)prepareForReceiving {
// create session
_session = [AVCaptureSession new];
//set image quality
if ([_session canSetSessionPreset: <your quality>]) {
[_session setSessionPreset: <your quality>];
}
_device = [AVCaptureDevice defaultDeviceWithMediaType: <your media type>];
NSError *err = nil;
// add default input for your media type
AVCaptureDeviceInput *inpt = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&err];
if (inpt) {
[_session addInput:inpt];
} else {
NSLog(@"Can't add input, cauz ---> %@",[err description]);
}
// create output
_vdo = [AVCaptureVideoDataOutput new];
NSDictionary *newSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
_vdo.videoSettings = newSettings;
dispatch_queue_t queue = dispatch_queue_create("newQueue", NULL);
[_vdo setSampleBufferDelegate:self queue:queue];
if ([_session canAddOutput:_vdo]) {
[_session addOutput:_vdo];
}
NSLog(@"Start video session");
[_session startRunning];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
// create image from sampleBuffer
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
UIImage *image = [[UIImage alloc] initWithCGImage:quartzImage
scale:1.0f
orientation:UIImageOrientationRight];
CGImageRelease(quartzImage);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// image = profit!
}