2011-11-22 42 views
0

我有一個應用程序,我將圖像保存到NSDocumentDirectory。我可以看到路徑設置正確,但是當我嘗試檢索它們時,NSData爲空。顯示iOS 5中保存的圖像5

下面是我的一些片斷..

NSData *imageData = UIImagePNGRepresentation(image); 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myApp"]; 

//Create unique filename 
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault); 
CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId); 
path = [path stringByAppendingPathComponent:(__bridge_transfer NSString *)newUniqueIdString]; 
path = [path stringByAppendingPathExtension: @"png"]; 

[imageData writeToFile:path atomically:NO];   

[self showPhoto:path]; 

然後在我的showPhoto方法

NSData *pngData = [NSData dataWithContentsOfFile:path]; 
UIImage *image = [UIImage imageWithData:pngData]; 

如果我調試pngData爲0字節,但我的路秀什麼,我認爲是正確的路徑?

任何調試方式,如果照片實際上是在電話上?我不知道它是否得到保存。

回答

1

你可以做這樣的事情,並檢查文件是否存在,並使用imageWithContentsOfFile:來檢索它。

NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:localFilePath]; 
bool fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath]; 

if(fileExists == YES) 
{ 
    UIImage *_image = [UIImage imageWithContentsOfFile:localFilePath] 
} 

如果圖像仍然爲空,那麼圖像出現問題。

+0

fileExists返回false。在模擬器和手機上是錯誤的。 – jdruid

+0

我刪除了獨特的身份證和額外的路徑思想搞砸了。由於某種原因未保存圖像。 – jdruid

+0

我建議你仔細檢查你保存文件的位置,因爲它看起來像你可能將它們保存到一個目錄並從另一個目錄中調用它。正如你可能已經猜測,如果「fileExists」返回false,那麼它意味着你試圖獲得的文件不存在。 –

1

如果您在模擬器中運行應用程序,你可以在這裏找到應用程序文件:/用戶/ {用戶} /庫/應用程序支持/ iPhone模擬器/ 5.0 /應用

如果你在iPhone上調試您可以使用iExplorer看iPhone上的文件。

+0

,使之有意義。這些文件在文檔目錄中找不到。 – jdruid