Domanda

Come aggiungere un overlay ( UIImageView ) all'anteprima della fotocamera e gestire i tocchi su questo?

I miei precedenti tentativi di farlo (ad es. utilizzare UIImagePickerController e aggiungere l'immagine come sottoview) non sono riusciti.

È stato utile?

Soluzione

Questo tutorial lo spiega: http://www.musicalgeometry.com/?p=821

Aggiungi semplicemente un UIImage nella vista overlay invece dell'area rossa mostrata nel tutorial.

Altri suggerimenti

Per il tuo file di implementazione:

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

Crea una classe UIView e aggiungi questo codice

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

Potresti essere in grado di aggiungere UIImageView come sottoview della finestra principale direttamente anziché UIImagePicker , potrebbe funzionare meglio. Assicurati di aggiungerli nel giusto ordine o chiama

[window bringSubviewToFront:imageView];

dopo aver acceso la fotocamera.

Se vuoi gestire i tocchi su UIImageView puoi semplicemente aggiungere UIImageView come sottoview di un normale View a schermo intero sfondo e aggiungi invece quello alla finestra, con una normale sottoclasse UIViewController che puoi usare per gestire gli eventi touch.

Guarda la vista overlay della fotocamera (disponibile in 3.1 e successive)

@property(nonatomic, retain) UIView *cameraOverlayView
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top