2015-07-29 4 views
1

Я пытался решить эту проблему с тех пор. У меня есть приложение, в котором пользователь выбирает изображение, чем выбранное изображение сохраняется на диске, а затем загружается в UIImageView, однако это не работает, несмотря ни на что. Я сделал небольшое приложение, чтобы проверить это, и он тоже не работает. ImageView остается пустым. Вот мой код:Сохраните изображение в каталоге Документов, чем загрузите его в UIImageView

@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 

Что я делаю неправильно?

ответ

0

Вам просто нужно добавить делегата из 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 
+0

Вам просто нужно делегировать 'UIImagePickerController'. –

+0

Да .. спасибо. Мне стыдно сейчас ... такая ошибка noob. Вы только что сохранили мой проект. Спасибо!! :)) –

Смежные вопросы