xcode UIImageView - 显示目录中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3448508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:37:31  来源:igfitidea点击:

UIImageView - Displaying an image from Directory

iphonexcodeuiimageview

提问by Stephen

I have the following code in place:

我有以下代码:

- (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' is defined as UIImageView. I'm trying to display the file in the path 'imagePath'. I know the code graph = imagePath is not correct, as the variable 'imagePath' states it contains the path to the image.

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

How would I display my image located at the specific image path ?

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

Regards, Stephen

问候, 斯蒂芬

回答by Swapnil Luktuke

You'll have to create an imageview object, set it as the image view's image and release the image you created:

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

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