UIImageView - 显示目录中的图像

Ste*_*hen 2 iphone xcode 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];
    }
Run Code Online (Sandbox Code Playgroud)

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

如何在特定图像路径上显示我的图像?

问候,斯蒂芬

Swa*_*uke 9

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

UIImage *graphImage = [[UIImage alloc] initWithContentsOfFile: imagePath];
graph.image = graphImage;
[graphImage release];
Run Code Online (Sandbox Code Playgroud)

  • 在设备上尝试...模拟器有时会很奇怪. (2认同)