【问题标题】:UIImage imageWithContentsOfFile does not work (ImageNamed works fine)UIImage imageWithContentsOfFile 不起作用(ImageNamed 工作正常)
【发布时间】:2014-06-18 13:12:32
【问题描述】:

我正在努力显示图像。

以下代码可以正常工作。

imageView.image = [UIImage imageNamed:@"picture"];

imageView.image = [UIImage imageNamed:@"picture.png"];

但是下面的代码不起作用。

NSString *theImagePath = [[NSBundle mainBundle] pathForResource:@"picture" ofType:@"png"];
imageView.image = [UIImage imageWithContentsOfFile:theImagePath];

imageView.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"picture" ofType:@"png"]];

我尝试清理项目,但尚未修复。 有没有人有同样的经历?

【问题讨论】:

  • 没有理由它不应该工作。该错误很可能出现在代码中的其他地方。还有,你为什么不想用imageNamed
  • 您是否尝试过分解其中一些电话?在使用 NSString 的情况下 *theImagePath = [[NSBundle mainBundle] pathForResource:@"picture" ofType:@"png"]; theImagePath 是否返回任何内容?
  • @PeterFoti 因为我收到内存警告,发现如果显示大图像或多次调用 imageNamed,imageNamed 会发出内存警告。
  • @RobSegal theImagePath 返回(空)。有什么想法吗?
  • @tomo 好吧,这绝对是有启发性的。您是否尝试过 NSString *theImagePath = [[NSBundle mainBundle] pathForResource:@"picture.png" ofType:@""]?您还应该检查打包的二进制文件,以确保您的图像确实在要找到的包中。

标签: ios7 uiimage xcode5


【解决方案1】:

imageView.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"picture.png" ofType:nil]];

试一试,Xcode 可能无法将您的文件识别为 PNG 文件。

但是你为什么不留在imageNamed:呢?

【讨论】:

  • 感谢您的回复。正如你所建议的,我试过了,但结果是一样的。
猜你喜欢
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 2011-04-13
  • 2014-10-06
  • 2017-05-03
  • 1970-01-01
相关资源
最近更新 更多