【问题标题】:UIImageView image not showing upUIImageView 图像未显示
【发布时间】:2012-08-20 19:08:30
【问题描述】:
_topBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];

UIImageView *bar = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"top_bar.png"]];
bar.frame = CGRectMake(0, 0, 320, 45);
[_topBar addSubview:bar];

_up = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"up.png"]];
[_topBar addSubview:_up];

_up 没有出现在我的构建中。如果我将“up.png”交换为资源文件夹中的任何其他图像,它会显示出来,但 up.png 拒绝显示。有什么我可以做的吗?我只使用 UIImage、静态分配的 UIImageView 对其进行了初始化,删除并重新添加了 .png 文件,将它们从资源文件夹中取出并将它们放在其他地方,重新保存它们,除了扔掉它们之外的一切。

编辑: 该文件保存为 up.png 但实际上是 up.png.psd。只是阅读细则的问题。

【问题讨论】:

  • 如果您在 Finder 中右键单击图像并获取信息,Kind 下会列出什么?
  • ...adobe photoshop 文件。谢谢先生,我将它保存为 up.png 但实际上从未将其更改为 png。
  • 附上评论的答案。快乐编码:)

标签: ios uiimageview uiimage


【解决方案1】:

在 Finder 中检查种类。它可能不是 png,因此不会显示。

自从您发表评论后,我现在知道这是真的:)

【讨论】:

  • 我对这个答案持怀疑态度,因为我的 UIImageView 已经正常工作了大约一周,然后随机就不再显示了。果然是JPG而不是PNG。奇怪的是它完全有效。
  • 不是很奇怪,UIImage 从二进制数据中读取,它根本不关心文件名。标头中的前几个字节足以确定它是否是有效的图像文件。
【解决方案2】:

有几件事。在右侧窗格中选择文件,打开最右侧(第 3 个)文件检查器窗格,并确保此图像包含在您的 Target 中。

在代码中,打破这个:

UIImage *image = [UIImage imageNamed:@"up.png"];
NSLog(@"image %@", image);
_up = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"up.png"]];
NSLog(@"UP FRAME: %@", NSStringFromCGRect(_up.frame) );
// maybe set the frame to what you want?
_up.frame = (CGRect){ {0,0}, image.size }; // set origin where you want it

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2011-02-08
    • 2011-06-01
    相关资源
    最近更新 更多