上创建带有方形视图的自定义相机

上创建带有方形视图的自定义相机

本文介绍了在 iOS 上创建带有方形视图的自定义相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 iOS 上创建自定义相机体验,以下代码片段是我所得到的.基本上我想要通常的相机视图(即使用以下按钮:捕获、闪光、网格、前/后、取消).但是普通相机和我的相机之间的唯一区别是我想要预览表面的正方形;不是矩形.然后,所见即所得(WYSIWYG),无需裁剪;因为用户首先会拍摄一张方形照片.

I am trying to create a custom camera experience on iOS and the following code snippet is as far as I got. Basically I want the usual camera view (i.e. with the following buttons: capture, flash, grid, front/back, cancel). But the only difference between the normal camera and mine is that I want a square for the preview surface; not a rectangle. And then, what you see is what you get (WYSIWYG) such that there is no cropping necessary; as the user would have taken a square picture in the first place.

我也一直在查看图书馆 https://github.com/danielebogo/DBCamera 但我不知道如何自定义它到我的目的.有什么帮助吗?谢谢.

I have also been looking at the library https://github.com/danielebogo/DBCamera but I am not seeing how to customize it to my end. Any help? Thanks.

到目前为止我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //Capture Session
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    //Add device
    AVCaptureDevice *device =
    [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //Input
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if (!input)
    {
        NSLog(@"No Input");
    }

    [session addInput:input];

    //Output
    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:output];
    output.videoSettings =
    @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };

    //Preview Layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    UIView *myView = self.view;
    previewLayer.frame = myView.bounds;
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:previewLayer];

    //Start capture session
    [session startRunning];
}

这是Xcode单视图项目中唯一的自定义代码

This is the only custom code in a single view project on Xcode

推荐答案

您有两种选择来做您想做的事,要么坚持使用并自定义 UIImagePickerController,要么使用 UIImagePickerController 创建自己的代码>AVFoundation.

You have two options for doing what you want, either stick with and customize a UIImagePickerController, or create your own by using the AVFoundation.

UIImagePickerController 确实提供了相当多的自定义选项,这个类似的线程有一些很好的信息:链接.

The UIImagePickerController does provide a fair bit of customization options, and this similar thread has some good information on that: link.

如果您仍然想自己制作,我建议您前往 Apple 文档并查看这个名为 AVCam 的演示项目:链接.但是,它比您可能需要的更深入,因此我也可以推荐此视频教程:链接.

If you still want to make your own, I suggest heading over to the Apple Documentation and checking out this demo project called AVCam: link. However, it's way more in-depth than you'll probably need so I can recommend this video tutorial as well: link.

如果选择最后一个选项,我想提一下,要使实际相机"适合您的 previewLayer 的框架,您可以将 videoGravity 设置为AVCaptureVideoPreviewLayerAVLayerVideoGravityResizeAspectFill.

If going for the last option, I would like to mention that to make the "actual camera" fit the frame of your previewLayer, you can set the videoGravity on the AVCaptureVideoPreviewLayer to AVLayerVideoGravityResizeAspectFill.

这篇关于在 iOS 上创建带有方形视图的自定义相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:15