Load a saved image from documents directory
ios, nsdocument, objective-c
Solution
The problem is that you're updating your UIImageView with the UIImage only upon viewDidLoad. You need to add an update once you are done obtaining it from the filesystem (preferably on a background thread). So it would look something like this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
UIImage *loadedImage = [self loadImage];
dispatch_async(dispatch_get_main_queue(),^{
ImageView.image = loadedImage;
});
});
I would also recommend to use names starting with lowercase characters for instance names, so you should rename ImageView -> imageView
Problem
I have used the code given here to save and load images It works fine when i use it together in one view controller, but when i use the saveImage method in one view controller and try to load the image in another view controller the image returned blank... In view controller A i use the following to save a image ``` - (void)saveImage: (UIImage*)image { NSLog(@"saveimage called"); if (image != nil) { NSLog(@"Image not null"); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* path = [documentsDirectory stringByAppendingPathComponent: @"test.png" ]; NSData* data = UIImagePNGRepresentation(image); [data writeToFile:path atomically:YES]; QrImageView.image = nil; } } ``` And in view controller say B I'm loading the image using.. ``` - (UIImage*)loadImage { NSError *error; NSFileManager *fileMgr = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* path = [documentsDirectory stringByAppendingPathComponent: @"test.png" ]; UIImage* image = [UIImage imageWithContentsOfFile:path]; // Write out the contents of home directory to console NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); return image; } ``` I also get the content of the file in console but i dont understand why the image is blank ``` 2013-07-06 14:13:19.750 YouBank[500:c07] Documents directory: ( "test.png" ) ``` Any idea what I'm doing wrong... ``` EDIT: ``` In the view controller B on the viewDidload method i do the following ``` - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. ImageView.image=[self loadImage]; ImageName.text = @"Cash Withdrawal"; } ```