简体   繁体   English

iPhone:相机预览叠加

[英]iPhone: Camera Preview Overlay

How do I add an overlay ( UIImageView ) to the camera preview and handle touches on this?如何向相机预览添加叠加层 ( UIImageView ) 并处理其上的触摸?

My previous attempts to do this (eg use UIImagePickerController and add the image as a subview) have failed.我以前尝试这样做(例如使用UIImagePickerController并将图像添加为子视图)都失败了。

This tutorial explains it: http://www.musicalgeometry.com/?p=821 本教程解释了它: http//www.musicalgeometry.com/? p = 821

Just add a UIImage in the overlay view instead of the red area shown in the tutorial. 只需在叠加视图中添加UIImage,而不是教程中显示的红色区域。

For your implementation file:对于您的实施文件:

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

Make a UIView class and add this code制作一个 UIView class 并添加此代码

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

You may be able to add the UIImageView as a subview of the main window directly instead of the UIImagePicker , it may work better.您可以直接将UIImageView添加为主要 window 的子视图,而不是UIImagePicker ,它可能会更好。 Just make sure to add them in the right order, or call只需确保以正确的顺序添加它们,或致电

[window bringSubviewToFront:imageView];

after the camera is up.相机升起后。

If you want to handle touches on the UIImageView you could just add the UIImageView as a subview of a normal fullscreen View with a transparent background, and add that to the window instead, with a normal UIViewController subclass that you can use to handle the touch events.如果你想处理UIImageView上的触摸,你可以将UIImageView添加为具有透明背景的普通全屏View的子视图,然后将添加到 window 中,使用普通的UIViewController子类来处理触摸事件.

See the camera overlay view (available in 3.1 and later)查看相机叠加视图(在 3.1 及更高版本中可用)

@property(nonatomic, retain) UIView *cameraOverlayView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM