2012-01-10 19 views
0

我創建了一個應用程序,用於在應用程序不被刪除時創建支付文件的本地存儲。如何調用本地圖像文件並將其顯示到imageviewcontroller

這我已經做了:我下載文件並存儲到本地(應用程序文檔文件夾)。

現在我遇到了這個問題: 雖然後下載圖像,然後我進入本地和該圖像不會顯示。在NSLog下面的URL生成,我無法顯示圖像。

/Users/username/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/F233ED88-1546-4FB0-BE93-0E0FB8C8C3D6/Documents/NationalMedalofArts-150x150.jpg

編輯:

NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", 
        @"/Users/username/Library/Application Support/iPhone Simulator/5.0/Applications/F233ED88-1546-4FB0-BE93-0E0FB8C8C3D6/Documents/NationalMedalofArts-150x150.jpg"]]; 
NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
NSLog(@"%@",imageData); 
UIImage *image = [UIImage imageWithData:imageData]; 
self.imgView.image = image; 

當NSLog的NSData的則是NULL顯示

+0

你嘗試過什麼,你有任何錯誤日誌,你能解釋遠一點?這是一個非常模糊的問題,很難回答。 – Emil 2012-01-10 06:48:40

+0

填寫URL,因爲該字符串看起來像一個路徑,而不是URL。我的意思是URL以「http:」或「file:」開頭,你的字符串只是本地文件系統中文件的路徑。 – Gabriel 2012-01-10 07:03:29

+0

NSData返回NULL – 2012-01-10 07:04:00

回答

3

這是更好的:

NSString *fileName = @"NationalMedalofArts-150x150.jpg"; 
NSString *path = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), fileName]; 
self.imgView.image = [UIImage imageWithContentsOfFile:path]; 
+1

它更短,但這並沒有讓它變得更好:) – Emil 2012-01-10 15:52:56

2

嘗試使用這樣的:

NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString* file = [documentsPath stringByAppendingPathComponent:@"NationalMedalofArts-150x150.jpg"]; 
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:file]; 
UIImage *image = [UIImage imageWithData:imageData]; 
self.imgView.image = image; 

當應用程序中,這就是圖像和那些各種各樣的事情被發現,你必須使用的第一行,找到路徑的文件,文件夾的文件夾訪問文件。上面的代碼應該適合你。

相關問題