【问题标题】:Save image to Documents directory than load it into UIImageView将图像保存到 Documents 目录而不是将其加载到 UIImageView
【发布时间】:2015-10-20 09:19:26
【问题描述】:

我几天以来一直在尝试解决这个问题。我有一个应用程序,用户选择一个图像,然后将所选图像保存到磁盘并稍后加载到 UIImageView 中,但是无论我尝试什么,这都不起作用。我制作了一个小应用程序只是为了测试它,它在那里也不起作用。 imageView 保持空白。这是我的代码:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)button:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.allowsEditing = NO;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *selectedImage = info[UIImagePickerControllerOriginalImage];
    [self saveImage:selectedImage];
    self.imageView.image = [self loadImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)saveImage: (UIImage*)image
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      @"image.png" ];
    NSData* data = UIImagePNGRepresentation(image);
    [data writeToFile:path atomically:YES];
}


- (UIImage*)loadImage
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      @"image.png" ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    return image;
}

@end

我做错了什么?

【问题讨论】:

    标签: ios uiimageview uiimage save load


    【解决方案1】:

    您只需添加UIImagePickerController 的委托,以便以下代码解决您的问题。

    - (IBAction)button:(id)sender {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.allowsEditing = NO;
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }
    

    现在,在 .h 文件中使用以下代码。

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
    @property(nonatomic,weak)IBOutlet UIImageView *imageView;
    
    @end
    

    【讨论】:

    • 你只需要委托UIImagePickerController
    • 是的..谢谢。我现在感到很惭愧……真是一个菜鸟的错误。你刚刚保存了我的项目。谢谢!! :))
    猜你喜欢
    • 2012-08-13
    • 2011-01-03
    • 2012-10-12
    • 2015-07-04
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2014-08-26
    • 2023-03-10
    相关资源
    最近更新 更多