Iphone UIImageView-显示目录中的图像

Iphone UIImageView-显示目录中的图像,iphone,xcode,uiimageview,Iphone,Xcode,Uiimageview,我有以下代码: - (void)viewDidLoad { NSString *homeDirectoryPath = NSHomeDirectory(); NSString *imagePath = [homeDirectoryPath stringByAppendingString:@"/graph.png"]; NSLog(@"Image: %@", imagePath); if (![[NSFileManager defaultManager] file

我有以下代码:

- (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”中显示文件。我知道代码graph=imagePath不正确,因为变量“imagePath”表示它包含图像的路径

如何显示位于特定图像路径的图像

问候,,
Stephen

您必须创建imageview对象,将其设置为图像视图的图像,然后释放您创建的图像:

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

谢谢您的回答,但是initContentsOfFile方法的用途是什么?我在initContentsOfFile中遇到了错误,但是当我使用initWithContentsOfFile时,它编译得很干净,但仍然不显示图像。抱歉。。我的错。这是一个输入错误,我想用文件的内容输入init。我在回答中已经改正了。initWithContentsOfFile只要图像路径正确就可以工作。感谢Lukya,图像仍然没有按预期显示,当我在日志文件中显示图像路径时,我得到“/Users/stephenconnolly/Library/Application Support/iPhone Simulator/3.1.3/Applications/261904F9-AF97-4C5C-A968-0E51CD2CD71/Documents/graph.png”。Graph.png存在于此位置,因此我不确定发生了什么。请在设备上尝试。。。模拟器有时行为怪异。