我正在尝试从文档目录加载 UIImage
并将其设置为 UIImageView
,如下所示:
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
中。从该目录加载上述内容是否正确?
我还尝试了一些其他方法:UIImage imageWithContentsOfFile
,以及基于 SO 响应的其他方法。
最佳答案
要获取您应该使用的文档目录:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *pngfile = [documentsDir stringByAppendingPathComponent:@"school.png"];
我不太确定您是否还需要附加“MyAppCustomDirectory”,但我不这么认为。
关于ios - 从文档目录加载 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13057294/