McMonty
McMonty

Reputation: 85

How to let user toggle between front facing and rear camera in UIImagePicker?

How to let user toggle between front facing and rear camera in UIImagePicker?

-(void)takePhoto {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
#if TARGET_IPHONE_SIMULATOR
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
#else
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif
imagePickerController.editing = YES;
imagePickerController.delegate = (id)self;
[self presentModalViewController:imagePickerController animated:YES];
 }

Upvotes: 0

Views: 342

Answers (2)

Ol Sen
Ol Sen

Reputation: 3368

can't be told enough. When writing code in Xcode you can right-click on any class, enum, method etc. to browse to the definition and read on. So when you right-click on UIImagePickerControllerSourceTypeCamera and "Jump to Definition" you will end up reading UIKit/UIImagePickerController.h

Protocol <UIImagePickerControllerDelegate> knows of an Enum with name UIImagePickerControllerCameraDevice that has two entrys, UIImagePickerControllerCameraDeviceRear or UIImagePickerControllerCameraDeviceFront

and later on the protocol tells, you have a

@property(nonatomic) UIImagePickerControllerCameraDevice cameraDevice;

available.

Meaning, you can set

imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;

and also defined in the protocol which your imagePickerController is following

@property(nonatomic) BOOL showsCameraControls;

so you can

imagePickerController.showsCameraControls = YES;

makes it easy for you and the users know how to handle that interface.

Upvotes: 2

user7014451
user7014451

Reputation:

More than happy to delete this if necessary, just trying to help....

I just tried my Swift-written app that uses UIImagePickerController, with this code that I haven';t touched since 2016. It looks similar to your code - except that allowsEditing = false, which to you means editing = NO. (I also check for a rear-facing camera to make sure it's not a simulator running the code.) To me, it looks like UIImagePickerController automatically gives the user a way to switch to the front-facing camera.

    if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
        picker.allowsEditing = false
        picker.sourceType = UIImagePickerController.SourceType.camera
        picker.cameraCaptureMode = .photo
        picker.modalPresentationStyle = .fullScreen
        present(picker,
                animated: true,
                completion: nil)
    } else {
        noCamera()
    }
}

Have you tried setting editing to NO?

Upvotes: 1

Related Questions