在相机iOS上绘制叠加层 [英] Draw overlay on camera iOS
问题描述
用户需要能够拍摄其ID的照片.我需要在相机视图中添加一个蓝框作为指导.指南在所有设备尺寸上都应具有相同的长宽比,并在标签上贴上说明.我可以使用UIImagePicker完成此操作吗?
Users need to be able to take photo of their ID. I need to add a blue frame to camera view as a guide. Guide should have same aspect ratio on all device sizes and fit a label with instructions. Can I accomplish this using UIImagePicker?
这是一些不完整的代码.谢谢你的帮助.
Here is some incomplete code. Thanks for any help.
UIImageView *overlayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
CGRect overlayRect = CGRectMake(0, 0, self.view.frame.size.height - 16, self.view.frame.size.width - 16);
[overlayImage setFrame:overlayRect];
[self.imagePicker setCameraOverlayView:overlayImage];
推荐答案
使用AVCaptureDevice
,AVCaptureSession
,AVCaptureVideoPreviewLayer
和AVCapturePhotoOutput
.
将AVCaptureDeviceInput
设置为捕获会话的输入,并将照片输出作为捕获会话的输出.用AVCaptureSession
初始化AVCaptureVideoPreviewLayer
并添加到view.layer
想法addSublayer
中.您可以从情节提要或以编程方式实例化的控制器中使用UIViewController
,添加覆盖图或有角的view.layer.borderWidth
或UIBezierPath
的图片.将控制器设置为AVCapturePhotoCaptureDelegate
,添加委托方法.使用capturePhoto(with:delegate:)
方法.享受.
Set AVCaptureDeviceInput
as input of capture session, and photo output as output of capture session. Initialize AVCaptureVideoPreviewLayer
with AVCaptureSession
and add to your view.layer
thought addSublayer
. You can use UIViewController
from storyboard or programmatically instantiated controller, add picture of overlay or cornered view.layer.borderWidth
or UIBezierPath
. Set up controller as AVCapturePhotoCaptureDelegate
, add delegate methods. Use capturePhoto(with:delegate:)
method. Enjoy.
这篇关于在相机iOS上绘制叠加层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!