67 votes

Face à la caméra dans uiimagepickercontroller

Je développe l'application pour caméra frontale dans iPad2 en utilisant les uiimagepickercontroller .

Lorsque je capture l'image, elle est retournée de gauche à droite.

Comment puis-je corriger cela?

 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    {
        UIImagePickerController *imgPkr = [[UIImagePickerController alloc] init];
        imgPkr.delegate = self;
        imgPkr.sourceType = UIImagePickerControllerSourceTypeCamera;
        imgPkr.cameraDevice=UIImagePickerControllerCameraDeviceFront;


        UIImageView *anImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"select%d.png",val]]];
        anImageView.frame = CGRectMake(0, 0, anImageView.image.size.width, anImageView.image.size.height);
        imgPkr.cameraOverlayView = anImageView;
        [theApp.TabViewControllerObject presentModalViewController:imgPkr animated:YES];
        [imgPkr release];
    }
 

76voto

Inder Kumar Rathore Points 13538

Hey checkout cette réponse et avant de demander s'il vous plaît faire une recherche.

Aussi, vous pouvez utiliser cela aussi

 - (void)didTakePicture:(UIImage *)picture
{
    UIImage * flippedImage = [UIImage imageWithCGImage:picture.CGImage scale:picture.scale orientation:UIImageOrientationLeftMirrored];

    picture = flippedImage;
}
 

19voto

Yonatan Betzer Points 151

J'ai eu le même problème et la solution ci-dessus ne m'a fait la moitié de la réponse, car l'utilisateur doit approuver le miroir de l'image avant de passer à la page suivante de mon app - où-je utiliser l'image capturée après retournement.

Pour résoudre cela, j'ai eu à inverser la vue de la caméra à chaque fois que je passe à l'avant face à la caméra:

- (IBAction)flipCamera:(id)sender {
if(cameraUI.cameraDevice == UIImagePickerControllerCameraDeviceFront)
{
    cameraUI.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
else {
    cameraUI.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
cameraUI.cameraViewTransform = CGAffineTransformScale(cameraUI.cameraViewTransform, -1,     1);     
}

Juste pour élargir sur cette grande réponse, quelques exemples de code complet, Dec2013, iOS7 / Xcode5. S'occupe de tout. Vous avez juste besoin d'une icône (cameraToggle.PNG dans l'exemple).

-(void)showTheDeviceCamera
    {
    if ( ! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] )
        return;

    // self.cameraController is a UIImagePickerController
    self.cameraController = [[UIImagePickerController alloc] init];
    self.cameraController.delegate = (id)self;
    self.cameraController.mediaTypes = @[(NSString *)kUTTypeImage];
    self.cameraController.allowsEditing = YES;
    self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:self.cameraController animated:YES completion:NULL];


        // Add front-rear toggle button MANUALLY, IF NECESSARY
        // (You seem to usually get it for free, on iPhone, but
        // need to add manually on an iPad.)

        UIView *buttonView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cameraToggle"]];
        [buttonView sizeToFit];

        buttonView.userInteractionEnabled = YES;
        [self.cameraController.view addSubview:buttonView];

        UITapGestureRecognizer *tap =
            [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_frontRearButtonClicked) ];
        tap.numberOfTapsRequired = 1;
        [buttonView addGestureRecognizer:tap];

        // we'll add it at the top right .. could be anywhere you want
        buttonView.center = CGPointMake(
                self.cameraController.view.frame.size.width-buttonView.frame.size.width,
                3.0 * buttonView.frame.size.height
                );

    }

-(void)_frontRearButtonClicked
    {
    [UIView transitionWithView:self.cameraController.view
        duration:1.0
        options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionFlipFromLeft
        animations:^{
            if ( self.cameraController.cameraDevice == UIImagePickerControllerCameraDeviceRear )
                self.cameraController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
            else
                self.cameraController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        } completion:NULL];
    }

10voto

Lucas Eduardo Points 3884

Comme les autres réponses, j'ai eu le même problème. Comme Yonathan Betzer mentionné, il suffit de retourner l'image finale est seulement la moitié de la réponse, parce que l'aperçu de l'image, affichée par le UIPickerController lorsque vous prenez une photo avec la caméra frontale, elle est toujours à l'envers (le miroir).

Yonatan Betzer anwser fonctionne très bien, mais il n'a pas dit comment, ni où mettre l'action pour changer la caméra de l'appareil.

Basée à certains codes à partir d'internet, j'ai réussi à remplacer UIImagePicker de classe et de corriger ce comportement indésirable, avec l'aide d'un appareil photo de superposition.

Vous pouvez vérifier le code dans mon github, dans le lien ci-dessous:

https://github.com/lucasecf/flipped-cam

Il suffit de saisir l' LEImagePickerController.h et .m de votre projet et l'utiliser exactement comme vous le feriez avec un UIImagePickerController.

6voto

Darren Points 3714

Juste pour ajouter comment je viens de réaliser cela sans sous-classer UIImagePickerController et sans ajouter de boutons supplémentaires à la vue caméra.

Écoutez simplement cette notification qui est déclenchée plusieurs fois à chaque changement de caméra:

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(cameraChanged:)
                                                 name:@"AVCaptureDeviceDidStartRunningNotification"
                                               object:nil];
 

Ensuite, utilisez cette méthode pour retourner la vue de la caméra:

 - (void)cameraChanged:(NSNotification *)notification
{
    if(imagePicker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
    {
        imagePicker.cameraViewTransform = CGAffineTransformIdentity;
        imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, -1,     1);
    } else {
        imagePicker.cameraViewTransform = CGAffineTransformIdentity;
    }
}
 

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X