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

前端之家收集整理的这篇文章主要介绍了Ios在Xcode中创建简单的相机叠加如何?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
正如标题所述,我需要创建一个非常简单的相机叠加,同时使用UI ImagePickerController拍照.我想在相机上添加非常简单的.png文件(如一个空盒子),但我无法弄清楚如何做.

我已经检查了这个网页上的大部分教程,但是不了解大部分教程.看起来很难,并且添加一个简单的.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"];
}

我应该在哪里实施重叠代码?我应该使用什么类,我该怎么办?

解决方法

UIImagePickerController有一个名为cameraOverlayView的属性.它是一个UIView *,所以你需要创建一个,并把你的PNG的背景,然后将它分配给你的选择器的这个属性.

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

(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,但其他所有内容都将保持不变.

猜你在找的iOS相关文章