How to allow the user to pick a photo from his camera roll or photo library?

后端 未结 7 1536
粉色の甜心
粉色の甜心 2021-01-02 01:11

I\'m making a little photo editing app for fun. Users must select a photo from their camera roll which will then be imported for modification.

How does this generall

相关标签:
7条回答
  • 2021-01-02 01:46

    SWIFT 2.0

    Thanks to William T. this worked for my on my UITapGestureRecognizer

    func selectPhoto(tap: UITapGestureRecognizer) {
        let picker = UIImagePickerController()
        picker.delegate = self
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
        alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: {
            action in
            picker.sourceType = .Camera
            picker.allowsEditing = true
            self.presentViewController(picker, animated: true, completion: nil)
        }))
        alert.addAction(UIAlertAction(title: "Photo Library", style: .Default, handler: {
            action in
            picker.sourceType = .PhotoLibrary
            picker.allowsEditing = true
            self.presentViewController(picker, animated: true, completion: nil)
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }
    

    I added the following to let me edit the photo after selecting it to both the .Camera and .PhotoLibrary:

    picker.allowsEditing = true
    
    0 讨论(0)
  • 2021-01-02 01:47

    This answer relevant on physical device ONLY!

    Access Camera:

    - (void)takePhoto {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    [self presentViewController:picker animated:YES completion:NULL];
    
    }
    

    Access Camera Roll:

    - (void)selectPhoto {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:NULL];
    
    
    }
    

    Implementing the Delegate Methods of UIImagePickerController:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
    }
    

    And This:

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
    }
    

    Also check for more info on this link

    0 讨论(0)
  • 2021-01-02 01:51

    The easiest way to do this is by using the UIImagePickerController in a simple alertView.

    For example, you want someone to tap on their profile picture and be able to set a new image from either their camera or their photo library.

    @IBAction func btnProfilePicTap(sender: AnyObject) {
        let picker = UIImagePickerController()
        picker.delegate = self
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {
            action in
            picker.sourceType = .camera
            self.present(picker, animated: true, completion: nil)
        }))
        alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: {
            action in
            picker.sourceType = .photoLibrary
            self.present(picker, animated: true, completion: nil)
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
    

    Then simply add the delegate and you're done.

    extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
            //use image here!
            dismiss(animated: true, completion: nil)
        }
    
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            dismiss(animated: true, completion: nil)
        }
    }
    

    Sorry, this example is in swift but I hope it still helps.

    EDIT: Updated for Swift 5.

    0 讨论(0)
  • 2021-01-02 01:56

    Here is an example app and a wrapper that gives you the Take Photo or Select from Library just like Facebook does. https://github.com/fulldecent/FDTake

    0 讨论(0)
  • 2021-01-02 02:03

    I worked on an application that allows user to select a personal image. I had two UIButtons which could help the user to pick a picture, whether it was from camera or library. It's something like this:

    - (void)camera {
    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        return;
    }
    UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    //Permetto la modifica delle foto
    picker.allowsEditing = YES;
    //Imposto il delegato
    [picker setDelegate:self];
    
    [self presentModalViewController:picker animated:YES];
    }
    - (void)library {
    //Inizializzo la classe per la gestione della libreria immagine
    UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //Permetto la modifica delle foto
    picker.allowsEditing = YES;
    //Imposto il delegato
    [picker setDelegate:self];
    
    [self presentModalViewController:picker animated:YES];
    }
    

    You have to implement the UIImagePickerControllerDelegate:

    @interface PickPictureViewController : UIViewController <UIImagePickerControllerDelegate>
    
    @implementation PickPictureViewController
    
    #pragma mark UIImagePickerController Delegate
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(pickedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
    [self dismissModalViewControllerAnimated:YES];
    }
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissModalViewControllerAnimated:YES];
    }
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{}
    

    Hope it helps! ;)

    0 讨论(0)
  • 2021-01-02 02:05

    Take a look at UIImagePickerController

    0 讨论(0)
提交回复
热议问题