21

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?

MegaManX
  • 8,766
  • 12
  • 51
  • 83
  • I am using UIBarButtonItem. With switch i mean change from rear to front camera animated, like in apple app where it is flipping horizontal. – MegaManX Oct 09 '12 at 08:47
  • Have a look at [`transitionFromView:toView:duration:options:completion:`](http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/clm/UIView/transitionFromView:toView:duration:options:completion:) – David Rönnqvist Oct 09 '12 at 08:52
  • UIImagePickerController does have view property, but i am not one that is making transition between views, that is done automatically after i set camera device property. – MegaManX Oct 09 '12 at 09:06

2 Answers2

25

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.

Pablo
  • 317
  • 5
  • 8
  • Thanks for the answer. It provides the right transition that I'm looking for but the transition between the camera views still flickers a bit (the cameraDevice isn't set before the transition is done). I'm hesitant to just increase the animation duration because that value might be different on older iPads. Any thoughts? I was thinking of taking a screen shot and transition from that to the image picker. – Kevin Zych Aug 23 '13 at 15:37
  • Yeh I have the same issue - the transform is a nice idea - but Apple gives you no completion callback so timing it is not accurate. – daihovey Feb 18 '14 at 07:05
  • 1
    The problem with this solution is that it also flips camera overlay view, which is not the way Apple does it in their own camera app. – HiveHicks Aug 20 '14 at 05:31
6

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!

Mr Stanev
  • 1,662
  • 1
  • 19
  • 26