【问题标题】:UIImage imageNamed not returning image object in iOS 11UIImage imageNamed在iOS 11中不返回图像对象
【发布时间】:2017-09-28 21:51:55
【问题描述】:

以下代码从path 返回一个图像对象,适用于 iOS 11 之前的版本:

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

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

【问题讨论】:

  • 您是否检查过 path 的设置是否正确?即,做这样的事情?:NSLog(@"path='%@'", path);
  • 是的,我有,这就是为什么这很奇怪。完全相同的代码在 iOS 8 中有效,但在 iOS 11 中无效。在两个版本中,我可以看到完全相同的路径。

标签: objective-c null uiimage ios11 imagenamed


【解决方案1】:

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

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

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

【讨论】:

  • 现在我更加困惑了。当我登录 [NSBundle.mainBundle pathForResource:@"foo" ofType:@"png" inDirectory:@"dir"] 时,图像位于 iOS 8 中,但不在 iOS 11 中(!?)。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多