简体   繁体   中英

UIImagePickerController Change Camera Source via delegate?

I would like to be able to change the UIImagePickerControllerCameraDevice via a button. The Button I would like to use is placed in an overlay that is showing over the top of the ImagePicker when it runs.

I have an inkling that this will require me to use a delegate but my knowledge on delegates is very minimal. could someone point me in the right direction?

Here is the code I am using for my UIImagePickerController "GetPhoto" which checks if the device has a camera and enables the correct camera at start depending on which you have available, and if you have none it gives you camera roll.

-(IBAction) getPhoto:(id) sender {
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if((UIButton *) sender == takePictureButton) {
        overlayViewController *overlay = [[overlayViewController alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerCameraDeviceFront]) {
                picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;

            }
            else {
                picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
            }

            picker.cameraOverlayView = overlay; 
       }
       else {
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            picker.allowsEditing = YES;
        }
    }
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

Since you are using a view controller associated to the overlay, you can define a property for the image picker controller in the overlay view controller, probably assign ed rather than retain ed and define the actions of the button within the overlay view controller to affect the image picker controller instance referenced by that property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM