简体   繁体   中英

Camera view as a subview in iOS

I'll start with saying that I am new to objective-c and iOS-programming.

What I want to do is to display camera as a part of a view, like a rectangle in the upper part of a screen, where should I start?

(What GUI-component for the "camera view"? AVCamCaptureManager or UIImagePickerController ?)

You can use the AVFoundation to do that. A good starting point is to see the WWDC videos (since 2011) related with AVFoundation and Camera.

The Apple's example code for AVCam project is a very good starting point.

Here's an example of what you can do.

First you need to instantiate a capture session:

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPreset1280x720;

Then you must create the input and add it to the session in order to get images from your device camera:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

    if (!input) {
        NSLog(@"Couldn't create video capture device");
    }
    [session addInput:input];

Then you make use of AVCaptureVideoPreviewLayer to present in a Layer the images from your device camera:

AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

Finally you just need to set the frame (portion of UI you need) of that specific layer and add it to the desired view and start the session capture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM