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

后端 未结 7 1611
粉色の甜心
粉色の甜心 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 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 
    
    @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! ;)

提交回复
热议问题