简体   繁体   English

相机视图作为iOS中的子视图

[英]Camera view as a subview in iOS

I'll start with saying that I am new to objective-c and iOS-programming. 我首先要说的是我不熟悉objective-c和iOS编程。

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 ?) (“摄像机视图”的GUI组件是什么? AVCamCaptureManagerUIImagePickerController ?)

You can use the AVFoundation to do that. 您可以使用AVFoundation来做到这一点。 A good starting point is to see the WWDC videos (since 2011) related with AVFoundation and Camera. 一个很好的起点是看到与AVFoundation和Camera相关的WWDC视频(自2011年起)。

The Apple's example code for AVCam project is a very good starting point. Apple的AVCam项目示例代码是一个非常好的起点。

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在图层中显示设备摄像头中的图像:

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. 最后,您只需设置该特定图层的框架(您需要的UI部分),并将其添加到所需视图并开始会话捕获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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