I am trying to figure to rather simple problem for a while now with no success. I am saving a file to my Documents directory on the Device and trying to load it using an Image View later on. I verified that the file is actually there. Why is my Image not showing?
Thanks in advance for your help.
Here is the code where I am trying to load the image into the ImageView:
-(void)loadFileFromDocumentFolder:(NSString *) filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString: filename] ];
NSLog(@"outputPath: %@", outputPath);
UIImage *theImage = [UIImage new];
[UIImage imageWithContentsOfFile:outputPath];
if (theImage)
{
display = [UIImageView new];
display = [display initWithImage:theImage];
[self.view addSubview:display];
}
}
There are somethings wrong with you code.
In this line you create a new
UIImageobject but you do nothing with it.This class method will return a
UIImageobject with the image load from the file.You do the same thing with the
UIImageView.Also there is not need to
[NSString stringWithString: filename]you just create an extra string which is not need sincefilenameis already a string.Your code should work like this: