ImagePicker捕获照片,但无法保存到相册

时间:2014-10-19 14:32:29

标签: ios objective-c uiimageview uiimagepickercontroller

我已经编写了这段代码来为我的应用制作个人资料照片,但它不起作用。一直在寻找不同的来源,无法找到任何来源。请帮忙。我可以拍一张照片,但它没有保存在相册中,并且照片无法上传或保存在配置文件的UIImageView上。

#import "ViewController.h"

@interface ViewController ()
{
    UIImage *originalImage;
}

@property(nonatomic, weak) IBOutlet UIImageView *selectedImageView;

@property(nonatomic, weak) IBOutlet UIBarButtonItem *saveButton;


@end

@implementation ViewController
@synthesize selectedImageView, saveButton;

- (IBAction)photoFromAlbum    
{
    UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
    photoPicker.delegate = self;
    photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:photoPicker animated:YES completion:NULL];

}

- (IBAction)photoFromCamera
{
    UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];

    photoPicker.delegate = self;
    photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:photoPicker animated:YES completion:NULL];

}

- (void)imagePickerController:(UIImagePickerController *)photoPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.saveButton.enabled = YES;


    originalImage = [info valueForKey:UIImagePickerControllerOriginalImage];
    [self.selectedImageView setImage:originalImage];

  //  [photoPicker dismissModalViewControllerAnimated:YES];
}


- (IBAction)saveImageToAlbum
{
    UIImageWriteToSavedPhotosAlbum(self.selectedImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *alertTitle;
    NSString *alertMessage;

    if(!error)
    {
        alertTitle   = @"Image Saved";
        alertMessage = @"Image saved to photo album successfully.";
    }
    else
    {
        alertTitle   = @"Error";
        alertMessage = @"Unable to save to photo album.";
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle
                                                    message:alertMessage
                                                   delegate:self
                                          cancelButtonTitle:@"Okay"
                                          otherButtonTitles:nil];
    [alert show];
}

- (UIImage*)loadImage
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];


     NSString* path = [documentsDirectory stringByAppendingPathComponent:
                          [NSString stringWithString: @"test.png"] ];
     UIImage* image = [UIImage imageWithContentsOfFile:path];
     return image;
}
@end

0 个答案:

没有答案