iPhone:照相机预览叠加(iPhone: Camera Preview Overlay)

2019-06-23 08:32发布

如何覆盖(添加UIImageView这个)为相机预览和处理触摸?

我以前尝试做到这一点(如使用UIImagePickerController并添加图像作为一个子视图)都失败了。

Answer 1:

本教程介绍它: http://www.musicalgeometry.com/?p=821

刚刚在重叠视图,而不是在教程中所示的红色区域中添加一个UIImage。



Answer 2:

为了您的实现文件:

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


Answer 3:

您可以对添加UIImageView作为主窗口的子视图,而不是直接的的UIImagePicker ,它可以更好地工作。 只要确保增加他们在正确的顺序,或拨打电话

[window bringSubviewToFront:imageView];

后摄像头向上。

如果你想处理的润色UIImageView你可以只添加UIImageView作为一个正常的全屏的子视图View具有透明背景,并到窗口来代替,与正常UIViewController子类,你可以用它来处理触摸事件。



Answer 4:

见(3.1及更高版本)时,相机重叠视图

@property(nonatomic, retain) UIView *cameraOverlayView


文章来源: iPhone: Camera Preview Overlay