【问题标题】:UIImageView - Displaying an image from DirectoryUIImageView - 显示目录中的图像
【发布时间】:2010-08-10 11:40:06
【问题描述】:

我有以下代码:

- (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];
    }

'graph' 被定义为 UIImageView。我正在尝试在路径“imagePath”中显示文件。我知道代码图 = imagePath 不正确,因为变量 'imagePath' 表明它包含图像的路径。

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

问候, 斯蒂芬

【问题讨论】:

    标签: iphone xcode uiimageview


    【解决方案1】:

    您必须创建一个 imageview 对象,将其设置为 image view 的图像并释放您创建的图像:

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

    【讨论】:

    • 感谢您的回答,但是 initContentsOfFile 方法的目的是什么?
    • 我在使用 initContentsOfFile 时遇到了错误,但是当我使用 initWithContentsOfFile 时,它​​编译得很干净,但仍然不显示图像。
    • 对不起..我的错。这是一个错字,我打算输入 initWithContentsOfFile。我已经在答案中更正了。只要图像路径正确,initWithContentsOfFile 应该可以工作。
    • 感谢 Lukya,图像仍未按预期显示,当我在日志文件中显示我的 imagePath 时,我得到 '/Users/stephenconnolly/Library/Application Support/iPhone Simulator/3.1.3/Applications /261904F9-AF97-4C5C-A968-0E51CDD2CD71/Documents/graph.png'。 Graph.png 存在于这个位置,所以我不确定发生了什么。
    • 在设备上试一试...模拟器有时表现得很奇怪。
    猜你喜欢
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多