Can I have a camera that is not modally presented? How?

I'm working on an app where I took over development from other people. The camera they present is presented modally after the user taps a button. I would like to have a camera that is a "permanent" view, like the Camera App in iOS. Programming guides always talk about presenting the camera modally, but other apps like Instagram have a camera that is permanently part of a view.

Can I accomplish this? How?

Thanks!

Upvotes: 3

Views: 290

Answers (1)

yinkou
yinkou

Reputation: 5766

Yes you can by using AVFoundation. Import these headers:

#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
#import <QuartzCore/QuartzCore.h>

And use this to create an AVCaptureVideoPreviewLayer and display it on your view.

// Get annd start session
    AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
    [captureSession startRunning];

    // Get preview layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    CGRect layerRect = CGRectMake(0, 0, 320, 460);
    [previewLayer setFrame:layerRect];

    // Get video device
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if (videoDevice) {
        NSError *error;
        AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];

        if (!error) {
            if ([captureSession canAddInput:videoIn]){
                [captureSession addInput:videoIn];
            }
        }
    }

    // Add layer to view
    [[[self view] layer] addSublayer:previewLayer];

Upvotes: 3

Related Questions