43

如何UIImageView在相机预览中添加叠加层 ( ) 并处理对此的触摸?

我之前的尝试(例如使用UIImagePickerController和添加图像作为子视图)失败了。

4

4 回答 4

37

本教程解释它:http ://www.musicalgeometry.com/?p=821

只需在叠加视图中添加一个 UIImage,而不是教程中显示的红色区域。

于 2010-01-12T13:06:31.173 回答
10

对于您的实施文件:

- (IBAction)TakePicture:(id)sender {

    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;

        // Delegate is self
        imagePicker.delegate = self;

        OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];     

        // Insert the overlay:
        imagePicker.cameraOverlayView = overlay;

       // Allow editing of image ?
        imagePicker.allowsImageEditing = YES;
        [imagePicker setCameraDevice:
        UIImagePickerControllerCameraDeviceFront];
        [imagePicker setAllowsEditing:YES];
        imagePicker.showsCameraControls=YES;
        imagePicker.navigationBarHidden=YES;
        imagePicker.toolbarHidden=YES;
        imagePicker.wantsFullScreenLayout=YES;

        self.library = [[ALAssetsLibrary alloc] init];

        // Show image picker
        [self presentModalViewController:imagePicker animated:YES];
    }

创建一个 UIView 类并添加此代码

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code


            // Clear the background of the overlay:
            self.opaque = NO;
            self.backgroundColor = [UIColor clearColor];

            // Load the image to show in the overlay:
            UIImage *overlayGraphic = [UIImage imageNamed:@"overlaygraphic.png"];
            UIImageView *overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
            overlayGraphicView.frame = CGRectMake(30, 100, 260, 200);
            [self addSubview:overlayGraphicView];

        }
        return self;
    }
于 2012-11-06T12:07:31.597 回答
3

您也许可以直接将 添加UIImageView为主窗口的子视图而不是UIImagePicker,它可能会更好。只需确保以正确的顺序添加它们,或致电

[window bringSubviewToFront:imageView];

相机升起后。

如果您想处理触摸,UIImageView您可以将其添加为具有透明背景UIImageView的普通全屏的子视图,然后将添加到窗口中,并使用可用于处理触摸事件的普通子类。ViewUIViewController

于 2009-06-16T13:28:41.290 回答
2

查看相机叠加视图(在 3.1 及更高版本中可用)

@property(nonatomic, retain) UIView *cameraOverlayView
于 2011-02-08T17:45:53.240 回答