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

查看:130
本文介绍了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的背景,然后将它分配给选择器

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天全站免登陆