How to load images from the Documents folder that have been saved dynamically?
I have problem with loading images from the Documents folder of iPhone application into the tableView.
In a separate request, I check on the serv开发者_开发技巧er all the images available and download them into the "images" folder under Documents. I am pretty sure that the images are saved correctly.
NSString *filePath = [imagesFolderPath stringByAppendingPathComponent:imageFileName];
[urlData writeToFile:filePath atomically:NO];
NSLog(@"Saved to file: %@", filePath);
2010-01-22 17:07:27.307 Foo[2102:207] Saved to file: /Users/Hoang/Library/Application Support/iPhone Simulator/User/Applications/6133A161-F9DC-4C92-8AE6-5651022EAA94/Documents/images/86_2.png
[NSBundle mainBundle] is not suitable for loading the images because at runtime, the application tries to connect to the server to download the images, they are not static.
But loading the images from Documents/images folder does not give me the image on the TableView.
static NSString *CellIdentifier = @"CellCategory";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
UIImageView *imageView = cell.imageView;
MenuItem *item = (MenuItem *) [arrayMenuItems objectAtIndex:indexPath.row];
cell.textLabel.text = item.descrizione;
NSString *strImagePath = [[imagesFolderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d_%d", item.idItem, item.idNode]] stringByAppendingPathExtension:@"png"];
NSLog(@"strImagePath: %@", strImagePath);
imageView.image = [[UIImage imageWithContentsOfFile:strImagePath] autorelease];
2010-01-22 17:07:42.842 Foo[2102:207] strImagePath: /Users/Hoang/Library/Application Support/iPhone Simulator/User/Applications/6133A161-F9DC-4C92-8AE6-5651022EAA94/Documents/images/86_2.png
Is there anyone having the same problem?
I have looked around in stackoverflow but have not succeeded.
Thanks in advance.
I've just had the same problem with loading images from application's Documents folder to UITableViewCell. This works:
[UIImage imageWithContentsOfFile:fullPath];
hardcoding the path is a bad idea since it can change during a redeploy,
try something like this.. NSString *imagessDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/images"];
and verify the file/image is even there with nsfilemanager
Edited: ANSWER
Be sure to check the response on NSData to see if there are images. In my case, all codes are ok, but the response from server give nothing. It is still able to save the image to the file on the documents/images folder, and still not raise any error, until I realized that all the images are not exist on the server. THAT WAS THE ERROR, not relates anything to the Documents folder
Original answer
There must be some problems with the initialization code of UIImage.
I have tried already three initialization functions for image having the path on the Documents directory.
But it just does not work.
Here you can see, the code always fall into the (exist) block, the first line is to load the image from the Documents/images directory, it always fails.
The second line inside the (exist) block, I tried to get the image from the bundle, it works perfect, but it is not what I want.
Fourth line of code, I get the original link of the images on the server and it gets what I nearly want. (Actually, what I want is to save all the images from the server into the Documents/images directory before hand, and then load them from there)
NSString *strImagePath = [[imagesFolderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d_%d", item.idItem, item.idNode]] stringByAppendingPathExtension:@"png"];
NSLog(@"strImagePath: %@", strImagePath);
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:strImagePath];
if (exists){
//UIImage *menuImage = [UIImage imageWithContentsOfFile:strImagePath];
//UIImage *menuImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ALT" ofType:@"png"]];
//imageView.image = menuImage;
NSString *strImagePathURL = [NSString stringWithFormat:@"http://foo.com/%d_%d.png", item.idItem, item.idNode];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strImagePathURL]];
imageView.image = [[UIImage alloc] initWithData:imageData];
}
else {
imageView.image = [UIImage imageNamed:@"ALT.png"];
}
精彩评论