ios 自定义拍照页面_创建在iOS的方形的观点的自定义照相机

本文介绍了一种在iOS上创建自定义相机体验的方法,重点在于调整预览区域为正方形,实现所见即所得(WYSIWYG)的拍摄效果,并提供了一些代码示例及可能的实现途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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.

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

}

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

解决方案

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

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

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值