从文档目录加载UIImage [英] Load UIImage from documents directory

查看:71
本文介绍了从文档目录加载UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文档目录加载 UIImage 并将其设置为 UIImageView ,如下所示:

I'm trying to load a UIImage from the documents directory and set it to a UIImageView as per below:

NSString  *pngfile = [[MyUtil getLocalDirectory] stringByAppendingPathComponent:@"school.png"];
NSLog(@"%@", pngfile);
if ([[NSFileManager defaultManager] fileExistsAtPath:pngfile]) {
    NSData *imageData = [NSData dataWithContentsOfFile:pngfile];            
    UIImage *img = [UIImage imageWithData:imageData];
    [schoolImage setImage:img];
}

但是,每当我尝试上述操作时,图像都不会加载。该图片位于 Documents / MyAppCustomDirectory / school.png 中。以上是否正确加载该目录?

However, whenever I try the above, the image never loads. The image is in Documents/MyAppCustomDirectory/school.png. Is the above correct to load from that directory?

我还尝试了其他几个: UIImage imageWithContentsOfFile ,等等基于SO响应的方法。

I also tried a few others: UIImage imageWithContentsOfFile, among other ways based on SO responses.

推荐答案

要获取文档目录,您应该使用:

To get the documents directory you should use:

NSArray  *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir  = [documentPaths objectAtIndex:0];
NSString  *pngfile = [documentsDir stringByAppendingPathComponent:@"school.png"];

我不太确定你是否还需要附加'MyAppCustomDirectory',但我不知道不这么认为。

I'm not quite sure if you also need to append the 'MyAppCustomDirectory', but I don't think so.

这篇关于从文档目录加载UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆