UIImage imageNamed在iOS 11中没有返回图像对象

时间:2017-09-28 21:51:56

标签: objective-c null uiimage ios11 imagenamed

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

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

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

2 个答案:

答案 0 :(得分:1)

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

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

NSString *path = [NSBundle.mainBundle pathForResource:@"foo" ofType:@"jpg" inDirectory:@"dir"];
UIImage *image = [UIImage imageWithContentsOfFile:path];

答案 1 :(得分:0)

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