2010-08-10 54 views
2

我已經到位以下代碼顯示圖像:的UIImageView - 從指南

- (void)viewDidLoad { 
    NSString *homeDirectoryPath = NSHomeDirectory(); 
    NSString *imagePath = [homeDirectoryPath stringByAppendingString:@"/graph.png"]; 
    NSLog(@"Image: %@", imagePath); 

    if (![[NSFileManager defaultManager] fileExistsAtPath:imagePath isDirectory:NULL]) 
    { 
     graph = imagePath; 
     //[[NSFileManager defaultManager] createDirectoryAtPath:imagePath attributes:nil]; 
    } 

「圖形」被定義爲的UIImageView。我試圖在路徑'imagePath'中顯示文件。我知道代碼圖= imagePath不正確,因爲變量'imagePath'指出它包含圖像的路徑。

如何顯示位於特定圖像路徑的圖像?

問候, 斯蒂芬

回答

9

你必須創建一個ImageView的對象,將其設置爲攝像畫面的圖像,並釋放所創建的圖像:

UIImage *graphImage = [[UIImage alloc] initWithContentsOfFile: imagePath]; 
graph.image = graphImage; 
[graphImage release]; 
+0

感謝您的回答,但有什麼initContentsOfFile方法的目的是什麼? – Stephen 2010-08-10 13:23:08

+0

我得到initContentsOfFile的錯誤,但是當我使用initWithContentsOfFile時,它編譯乾淨,但仍然不顯示圖像。 – Stephen 2010-08-10 13:58:49

+0

其他人對此有何想法? – Stephen 2010-08-10 16:06:52