2010-08-10 56 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