A
A
Artyom2015-08-01 12:58:45
Objective-C
Artyom, 2015-08-01 12:58:45

How to display the image from the camera on several different UIViews?

Tell me how to display a preview from the camera on several views, like here:
b5ad30d598d243b084da3aac2fd95f7a.jpg
I use AVFoundation

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
ManWithBear, 2015-08-01
@artyomabramov

We create a video session, become a delegate: and then we get a picture from the sample. Then you can work with this picture as you like.

- (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!
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question