Ios 如何在 Xcode 中创建简单的相机叠加层?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12572113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:22:06  来源:igfitidea点击:

Ios creating simple camera overlay in Xcode how?

iosxcodecamera

提问by bugra sezer

As the title says, I need to create a very simple camera overlay while taking photos with UIImagePickerController. I want to add very simple .pngfile (like an empty box) over the camera but I can't figure out how to do it.

正如标题所说,我需要在使用UIImagePickerController. 我想.png在相机上添加非常简单的文件(如一个空盒子),但我不知道该怎么做。

I've checked most of the tutorials in this webpage but don't understand most of them. It seems very hard to me and adding one simple .pngfile to the camera overlay should be easier than that.

我已经检查了这个网页中的大部分教程,但大部分都不理解。对我来说似乎很难,.png向相机叠加添加一个简单的文件应该比这更容易。

(IBAction) getPhoto:(id) sender {
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if((UIButton *) sender == choosePhotoBtn) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    } else {

    }

    label1.text =@"PHOTO ACTION";

    [self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

Where should I implement my overlay code? What classes should I use and how can I do it?

我应该在哪里实现我的覆盖代码?我应该使用哪些课程以及如何使用?

回答by dreamzor

There is a property of UIImagePickerControllercalled cameraOverlayView. It's an UIView*, so you need to create one and put your PNG to the background of it, then assign it to this property of your picker.

有一个UIImagePickerController名为的属性cameraOverlayView。它是一个UIView*,所以你需要创建一个并将你的 PNG 放在它的背景中,然后将它分配给你的picker.

Ok, here's the code (i didnt test it so)

好的,这是代码(我没有测试过)

(IBAction) getPhoto:(id) sender 
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    // creating overlayView
    UIView* overlayView = [[UIView alloc] initWithFrame:picker.view.frame];
    // letting png transparency be
    overlayView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourimagename.png"]];
    [overlayView.layer setOpaque:NO];
    overlayView.opaque = NO;

    picker.showsCameraControls = NO;
    picker.cameraOverlayView = overlayView;

    if((UIButton *) sender == choosePhotoBtn) 
    {
         if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
         {
             [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
         } else {
             //do something if the device has no camera or if the camera is disabled in settings (it cannot be assumed that the camera is available/not broken)
         }
    } else {

    }

    label1.text =@"PHOTO ACTION";

    [self presentModalViewController:picker animated:YES];
}

Of course, you can use some custom UIView*subclass instead of creating standard UIView* overlayView, but everything else will remain the same.

当然,您可以使用一些自定义UIView*子类而不是创建标准UIView* overlayView,但其他一切都将保持不变。