Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePickerController how to do animated switch from rear to front camera?

I have been using custom overlay for UIImagePickerController controller, and everything is working fine. I have added button to switch between front and rear camera via -

 - (IBAction)changeCamera:(id)sender {
if (self.imagePicker.cameraDevice == UIImagePickerControllerCameraDeviceRear) {
    self.imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
else {
    self.imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
}

Problem is, switch is not animated. I have been using apple camera app which is built on top of UIImagePicker, and the switch is happening animated. How do I do this?

like image 385
MegaManX Avatar asked Oct 09 '12 08:10

MegaManX


2 Answers

I was trying to do this today, and I was able to get it working with the following code:

[UIView transitionWithView:imagePickerController.view duration:1.0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionFlipFromLeft animations:^{
            imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        } completion:NULL];

I hope this helps anyone who comes across this question.

like image 80
Pablo Avatar answered Nov 06 '22 22:11

Pablo


Here's the Swift code for the accepted answer in case someone needs it:

UIView.transitionWithView(imagePickerController.view!, duration: 1.0, options: [.AllowAnimatedContent, .TransitionFlipFromLeft], animations: {() -> Void in
            imagePickerController.cameraDevice = .Rear
            }, completion: nil)

Thanks Pablo!

like image 32
Mr Stanev Avatar answered Nov 06 '22 22:11

Mr Stanev