2010-01-22 31 views
2

我有問題從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

1)檢查writeToFile的返回值。 2)用於調試檢查文件是否存在於strImagePath和NSFileManager fileExistsAtPath中。 – zaph 2010-01-22 16:56:31

+0

我已經檢查了圖像目錄中的圖像:NSLog([[NSFileManager defaultManager] directoryContentsAtPath:imagesFolderPath],nil)。有所有下載的圖像。 – 2010-01-22 17:02:31

+0

哦,事實上,[[NSFileManager的defaultManager] fileExistsAtPath:strImagePath]返回YES,所以必須有一些其他問題與tableView不會將圖像加載到表中:-s – 2010-01-22 17:09:27

回答

0

編輯:ANSWER

請務必檢查NSData的反應,看看是否有圖像。在我的情況下,所有的代碼都可以,但是服務器的響應沒有任何結果。它仍然能夠將圖像保存到文件/圖像文件夾中的文件,並且仍然不會引發任何錯誤,直到我意識到所有圖像都不存在於服務器上。那是錯誤,不涉及任何的文檔文件夾

原來的答覆

必須有一些問題的UIImage的初始化代碼。

我已經嘗試過三個初始化函數,用於具有Documents目錄路徑的圖像。

但它只是不起作用。

在這裏你可以看到,代碼總是落在(存在)塊中,第一行是從Documents/images目錄加載圖像,它總是失敗。

(存在)塊內的第二行,我試圖從包中獲取圖像,它工作完美,但它不是我想要的。

第四行代碼,我得到了服務器上的圖像的原始鏈接,它得到了我幾乎想要的。 (其實,我要的是所有來自服務器的圖像保存到手上收到的文件/ images目錄,然後從那裏加載它們)

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"]; 
} 
+0

嗯,答案是服務器上的圖像不存在:-s – 2010-02-11 14:12:44

0

硬編碼的路徑是一個糟糕的主意,因爲它可以在改變重新部署,

嘗試類似這樣的.. NSString * imagessDirectory = [NSHomeDirectory()stringByAppendingPathComponent:@「Documents/images」];

和驗證文件/圖片甚至還有用的NSFileManager

+0

嗨,在我的代碼中,我正在硬編碼路徑,正如你所說的?生成的路徑是控制檯上的日誌 – 2010-01-24 10:06:45

5

我剛剛從應用程序的文件夾到的UITableViewCell加載圖像同樣的問題。這工作:

[UIImage imageWithContentsOfFile:fullPath]; 
相關問題