Search code examples
iosobjective-cuiimageuiimagepickercontroller

Taking a photo and saving it to the Camera Roll


I'm having a problem where I just want a simple solution to take a picture and then save it in my app. However so far all I can do is take the photo have it load in a UIImageView, however it does not save in the Camera Roll.

- (IBAction)takePhoto:(UIButton *)sender 
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}  

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

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

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

Any help regarding this would be greatly appreciated. Believe me, I really need it.


Solution

  • You will want to do this:

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