Ios在Xcode中创建简单的相机叠加怎么样?

时间:2012-09-24 20:05:33

标签: ios xcode camera

正如标题所说,我需要在使用UIImagePickerController拍照时创建一个非常简单的相机叠加层。我想在相机上添加非常简单的.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"];
}

我应该在哪里实施我的重叠代码?我应该使用哪些课程,我该怎么做?

1 个答案:

答案 0 :(得分:15)

UIImagePickerController有一个名为cameraOverlayView的属性。它是UIView*,因此您需要创建一个PNG并将其置于其背景中,然后将其分配给picker的此属性。

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

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