ios如何在Xcode中创建简单的相机覆盖? [英] Ios creating simple camera overlay in Xcode how?

查看:26
本文介绍了ios如何在Xcode中创建简单的相机覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我需要在使用 UIImagePickerController 拍照时创建一个非常简单的相机叠加层.我想在相机上添加非常简单的 .png 文件(如一个空框),但我不知道该怎么做.

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

我查看了此网页中的大部分教程,但其中大部分内容并不了解.对我来说这似乎很难,将一个简单的 .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 .png file to the camera overlay should be easier than that.

(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?

推荐答案

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

There is a property of UIImagePickerController called 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.

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

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

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

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

这篇关于ios如何在Xcode中创建简单的相机覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆