我有問題從iPhone應用程序的Documents文件夾加載到tableView中的圖像。如何從動態保存的Documents文件夾中加載圖像?
在一個單獨的請求中,我檢查服務器上所有可用的圖像,並將它們下載到文檔下的「圖像」文件夾中。我很確定圖像保存正確。
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
有沒有人有同樣的問題?
我在環視瞭望,但沒有成功。
在此先感謝。
1)檢查writeToFile的返回值。 2)用於調試檢查文件是否存在於strImagePath和NSFileManager fileExistsAtPath中。 – zaph 2010-01-22 16:56:31
我已經檢查了圖像目錄中的圖像:NSLog([[NSFileManager defaultManager] directoryContentsAtPath:imagesFolderPath],nil)。有所有下載的圖像。 – 2010-01-22 17:02:31
哦,事實上,[[NSFileManager的defaultManager] fileExistsAtPath:strImagePath]返回YES,所以必須有一些其他問題與tableView不會將圖像加載到表中:-s – 2010-01-22 17:09:27