簡體   English   中英

iPhone:相機預覽疊加

[英]iPhone: Camera Preview Overlay

如何向相機預覽添加疊加層 ( UIImageView ) 並處理其上的觸摸?

我以前嘗試這樣做(例如使用UIImagePickerController並將圖像添加為子視圖)都失敗了。

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

只需在疊加視圖中添加UIImage,而不是教程中顯示的紅色區域。

對於您的實施文件:

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

您可以直接將UIImageView添加為主要 window 的子視圖,而不是UIImagePicker ,它可能會更好。 只需確保以正確的順序添加它們,或致電

[window bringSubviewToFront:imageView];

相機升起后。

to the window instead, with a normal UIViewController subclass that you can use to handle the touch events.如果你想處理UIImageView上的觸摸,你可以將UIImageView添加為具有透明背景的普通全屏View的子視圖,然后將添加到 window 中,使用普通的UIViewController子類來處理觸摸事件.

查看相機疊加視圖(在 3.1 及更高版本中可用)

@property(nonatomic, retain) UIView *cameraOverlayView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM