在iOS中将相机视图作为子视图

3
我要开始说,我是objective-c和iOS编程的新手。
我想做的是将相机显示为视图的一部分,就像屏幕上方的矩形,从哪里开始?
(用于“相机视图”的GUI组件是什么?AVCamCaptureManager还是UIImagePickerController?)

@user1354603 你需要创建相机叠加视图。 - Anand Gautam
我完全不同意“过于宽泛”的评级。这恰好是我的问题(对于任何尝试过的iOS程序员来说都很清楚),而答案完美地符合了SO的格式。(我现在有可工作的代码。我很想将其添加为答案。)请重新打开。 - Olie
1个回答

4
你可以使用AVFoundation来实现这个功能。一个很好的起点是查看与AVFoundation和相机相关的WWDC视频(自2011年以来)。
Apple的AVCam项目示例代码是一个非常好的起点。
以下是你可以做的一个示例。
首先,你需要实例化一个捕获会话:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPreset1280x720;

然后,您必须创建输入并将其添加到会话中,以便从设备相机获取图像:

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];

然后,您可以利用AVCaptureVideoPreviewLayer在一个层中呈现来自设备摄像头的图像:
AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

最后,您只需设置特定图层的框架(所需UI部分),将其添加到所需视图并开始会话捕获。

谢谢您的回复,我会尝试并返回结果。 - user1354603

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接