创建在iOS的方形的观点的自定义照相机 [英] creating custom camera with square view on iOS
问题描述
我想在iOS上创建一个自定义的相机体验,下面的代码段是我的。基本上我想要通常的相机视图(即具有以下按钮:捕获,闪光,网格,前/后,取消)。但正常的摄像机和我的唯一的区别是,我想要一个正方形的预览表面;不是矩形。然后,你看到的是你得到的(所见即所得),使得没有必要的耕作;
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.
我的代码SO FAR:
MY CODE SO FAR:
- (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 $ c $
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 code>确实提供了一些公平的定制选项,这个类似的线程有一些很好的信息:链接。
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
,您可以在 AVCaptureVideoPreviewLayer
上设置 videoGravity
AVLayerVideoGravityResizeAspectFill
。
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的方形的观点的自定义照相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!