2014-10-09 116 views
0

我有一部分應該將圖像保存到手機的「文檔」文件夾中,然後進行訪問。它可以很好地保存,但加載方法並不真正起作用。這裏是我的.m代碼:無法從iPhone/iPhone模擬器的「文檔」文件夾中加載圖像

- (UIImage*)loadImage 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *path = [documentsDirectory stringByAppendingPathComponent: 
          @"photo.png" ]; 
     UIImage *image = [UIImage imageWithContentsOfFile:path]; 
     return image; 
     studentImage = [[UIImageView alloc] initWithImage: image]; //UIImageView property 
    } 

- (UIImage*)loadBarcode 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString* path = [documentsDirectory stringByAppendingPathComponent: 
          @"barcode.png" ]; 
     NSLog(@"%@", path); 
     UIImage *image = [UIImage imageWithContentsOfFile:path]; 
     return image; 
     barcode = [[UIImageView alloc] initWithImage: image]; // UIImageView property 
    } 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self loadBarcode]; 
    [self loadImage]; 

} 

這些文件肯定有訪問權限,文件路徑是正確的。但是我試圖將它們分配給它的UIImageView呈現空白。我沒有選擇。你們有什麼建議嗎?

+0

當您手動轉到文件路徑並打開圖像時,它是否真的有內容? – TMob 2014-10-09 06:21:03

+0

你有返回類型UIImage的方法,並且你沒有使用這些方法的返回對象。此外,這些方法中的最後一行將不會被執行,因爲您在最後一行之前返回。 – 2014-10-09 06:24:20

+0

返回圖像聲明應該在您的代碼中將圖像設置爲imageview後最後。因爲在調用函數時不需要返回語句。使該功能爲(void) – Max 2014-10-09 06:25:23

回答

0

loadImage方法,問題就在這裏:

return image; 
studentImage = [[UIImageView alloc] initWithImage: image]; 

您第一次使用return聲明,傳輸控制,從來沒有把它分配給UIImageView控制。

它應該是這樣的:

studentImage = [[UIImageView alloc] initWithImage: image]; 
return image; 

還有一點,如果studentImage通過IB,DON'T re-allocate it連接。相反使用:

studentImage.image = image; 

希望它有幫助!

+0

這幫助我找到答案!我非常糟糕地搞亂了我的屬性,這影響了我的代碼。非常感謝你的回答! – 2014-10-09 06:44:51

0

現在問題已修復,是我的屬性名稱的問題,但感謝您幫助我清理我的代碼!

相關問題