我在将 iPhone 应用程序的 Documents 文件夹中的图像加载到 tableView 时遇到问题。
在一个单独的请求中,我在服务器上检查所有可用图像并将它们下载到“文档”下的“图像”文件夹中。我非常确定图像已正确保存。
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] 不适合加载图像,因为在运行时,应用程序会尝试连接到服务器来下载图像,它们不是静态的。
但是从 Documents/images 文件夹加载图像不会在 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
有人遇到同样的问题吗?
我在 stackoverflow 上查了一下,但没有成功。
提前致谢。
最佳答案
我刚刚将图像从应用程序的文档文件夹加载到 UITableViewCell 时遇到了同样的问题。这有效:
[UIImage imageWithContentsOfFile:fullPath];
关于iphone - 如何从Documents文件夹中加载动态保存的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2118709/