Enabling the photo library button on the UIImagePickerController

后端 未结 5 1962
庸人自扰
庸人自扰 2021-02-02 13:26

Does anyone know how to enable the photo album button on the UIImagePickerController when its in the camera mode? Like how the camera app on on the iphone can toggle between ima

5条回答
  •  野的像风
    2021-02-02 14:18

    I have done this tweaking Apple's PhotoPicker example app. I have removed all the camera controls and added my own button. When clicked, UIImagePickerControllerSourceType is set to the UIImagePickerControllerSourceTypePhotoLibrary.

    The tricky part for me was "dismissing" (might be technically the wrong word) the Photo Library after the image was picked. I did this by setting the source type back to UIImagePickerControllerSourceTypeCamera. This brings back the camera overlay view.

    ViewController.h
    
    #import 
    #import 
    #import 
    
    
    @interface ViewController : UIViewController  {
    
    //
    
    }
    
    @property (nonatomic, strong) UIImagePickerController *imagePicker;
    - (IBAction)uploadNewPhotoTapped:(id)sender;
    @end
    
    
    ViewController.m
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
     //Other code
    
    - (IBAction)uploadNewPhotoTapped:(id)sender {
    
        UIImagePickerController *imagePickController=[[UIImagePickerController alloc]init];
        //You can use isSourceTypeAvailable to check
    
        if ([UIImagePickerController    isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            imagePickController.sourceType=UIImagePickerControllerSourceTypeCamera;
            imagePickController.showsCameraControls=YES;
            //  self.usingPopover = NO;
        }
        else if ([UIImagePickerController  isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {//Check PhotoLibrary  available or not
            imagePickController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
            imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) //Check front Camera available or not
            imagePickController.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        //else //!!!!!!!!!!!exception
    
        imagePickController.delegate=self;
        imagePickController.allowsEditing=NO;
    
        [self presentModalViewController:imagePickController animated:YES];
    }
    
    
    - (void)imagePickerController:(UIImagePickerController *)picker  didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
        UIImage *originalImage=[info objectForKey:UIImagePickerControllerOriginalImage];
    
        //Do whatever with your image   
        NSData *data = UIImageJPEGRepresentation (
                                              originalImage,
                                              1.0
                                              );
    
        [self dismissModalViewControllerAnimated:YES];
    }
    
       // Other code
       @end
    

提交回复
热议问题