1

以下代码从 iOS 11 返回一个图像对象path并在 iOS 11 之前工作:

NSString *path = [anotherPath stringByAppendingPathComponent:file];
UIImage *image = [UIImage imageNamed:path];

但是在 iOS 11 中,image返回null. 这是 iOS 11 的错误吗?

4

2 回答 2

1

+[UIImage imageNamed:]记录为采用文件名,而不是路径。如果要从路径加载图像,请使用+[UIImage imageWithContentsOfFile:].

如果您在此处的路径指的是捆绑包中嵌套文件夹中的某些内容,则可以询问NSBundle资源的路径,然后将其传递给+[UIImage imageWithContentsOfFile:]. 这看起来像

NSString *path = [NSBundle.mainBundle pathForResource:@"foo" ofType:@"jpg" inDirectory:@"dir"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
于 2017-09-28T22:41:02.683 回答
0

正如@drewster 指出的那样,路径不正确;我有一个小写字符。但是,我注意到 iOS 8 忽略了这一点。

于 2017-09-30T16:27:09.830 回答