2012-03-03 52 views
1

向下滾動至編輯1.現在,這個最高位是無關緊要的。爲什麼這段代碼可能會讓我空?

這是我的代碼:

NSMutableDictionary *dictionary = [self.media objectAtIndex:index]; 
    NSLog(@"dictionary: %@", dictionary); 
    NSString *originalImagePath = [dictionary objectForKey:@"OriginalImage"]; 
    NSLog(@"path: %@", originalImagePath); 
    UIImage *originalImage = [UIImage imageWithContentsOfFile:originalImagePath]; 
    NSLog(@"original image: %@", originalImage); 
    return originalImage; 

而且我的NSLog:

dictionary: { 
    IsInPhotoLibrary = 1; 
    MediaType = 0; 
    OriginalImage = "/var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/E9904811-B463-4374-BD95-4AD472DC71A6.jpg"; 
} 
path: /var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/E9904811-B463-4374-BD95-4AD472DC71A6.jpg 
original image: (null) 

任何想法,爲什麼這可能是,現身爲空,儘管看似是一切就緒?

編輯:

這是寫的圖像文件的代碼:

+(NSString *)writeImageToFile:(UIImage *)image { 
    NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f); 

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images/"]; 
NSString *name = [NSString stringWithFormat:@"%@.jpg", [JEntry generateUuidString]]; 
NSString *filePath = [path stringByAppendingPathComponent:name]; 
[fullImageData writeToFile:filePath atomically:YES]; 
NSLog(@"original image 2: %@", [UIImage imageWithContentsOfFile:filePath]); 
} 

這也NSLog的出來爲空。所以問題出在這裏,很可能。有任何想法嗎?

編輯2:

所以,回到跟蹤更了,我已經意識到,這是因爲這個失敗:

[fullImageData writeToFile:filePath atomically:YES]; 

您可以在該行返回一個布爾值,告訴它是否是否成功,並且對我來說它沒有迴歸。任何想法,爲什麼這可能是?編輯3:

傳入的圖像是NULL。試圖找出現在哪裏出了問題。

+0

我不認爲在形象,路徑是正確的,大概增長沒有得到它正確 – 2012-03-03 12:12:37

+0

你的意思是它可能指向沒有圖像的位置,或者文件字符串看起來不正確? – Andrew 2012-03-03 12:19:05

回答

3

幾乎可以肯定你的文件不存在。修改您的代碼,如下所示以記錄文件是否存在:

NSString *originalImagePath = [dictionary objectForKey:@"OriginalImage"]; 
NSLog(@"path: %@", originalImagePath); 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:originalImagePath]; 
NSLog(@"file exists: %d", fileExists); 
+0

真的是gud答案,+ 1來這.. .. !!! – Kamarshad 2012-03-03 12:33:59

2

可能的原因:

  • 在originalImagePath該文件不存在。
  • 的UIImage不能從指定文件初始化的圖像時,因爲數據已損壞(也許的數據是空的或不完整的)
  • 文件不能因爲IOS文件的權限進行訪問(即,訪問超出該應用文件時沙箱)
1

您是否檢查過映像文件是否放在字典中的路徑上?您可以使用iExplorer實用程序。

還要注意文件名的大小寫,所以它的擴展名是'jpg'而不是'JPG'。

最後,你應該檢查,無論它是一個有效的圖像文件,通過打開它與一些圖像查看器。

1

您確定該文件夾中有jpg文件「E9904811-B463-4374-BD95-4AD472DC71A6.jpg」嗎? 嘗試打開安裝在iPhone/iPad模擬器的應用程序文件點擊yourFile.app 與CTRL鍵,並選擇打開包內容打開您的文件夾圖像...

得到了快速鏈接到您的文件夾:

NSArray *dirPaths; 
NSString *docsDir; 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
               NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 
NSLog(@"doc:%@",docsDir); 

您的應用程序應該在它的父文件夾