当我在 iOS 设备上运行应用程序时,图像不显示
我的应用程序中有这样标题的图像:
image~iPhone.png image@2x~iPhone.png
在界面生成器中,我将 image.png 加载到我的 UIImageView
中。我还使用 imageWithContentsOfFile 以编程方式将一些图像加载到不同的视图中。当我在模拟器中运行时,图像全部加载正常,但当我在设备上运行时,我没有得到图像。如果我在界面生成器中使用图像的全名,它可以工作,但我希望 iOS 能够区分高分辨率和低分辨率。我尝试了很多不同的事情,但无法弄清楚这一点。我也在调试器中看到此错误:
无法加载标识符为“com.mycompany.myproject”的捆绑包中的笔尖引用的“image.png”图像
Xcode 4 部署目标4.1 Base SDK 4.3
感谢您的帮助。
I have images titled like so in my app:
image~iPhone.png
image@2x~iPhone.png
In interface builder I am loading image.png into my UIImageView
. I also programatically load some images into a different view using imageWithContentsOfFile. The images all load fine when I run in the simulator but I get no images when I run on the device. If I use the full name of the image in interface builder it works but I want iOS to distinguish between high res and lower res. I have tried a lot of different things but can't figure this out. I see this error in the debugger as well:
Could not load the "image.png" image referenced from a nib in the bundle with identifier "com.mycompany.myproject"
Xcode 4
Deployment Target 4.1
Base SDK 4.3
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧……经过多次实验,我成功了。
我有两个名为:
图像,我尝试使用 IB 或 imageWithContentsOfFile 使用
加载它们,这在模拟器中运行良好,但在我的设备上运行不佳。我只是在图像应该在的地方出现了一个空白的白色屏幕。
我最终将高分辨率图像重命名为:
移动 '引用我的图像时,设备修饰符(〜iPhone)之后的@2x'修饰符允许它工作我从阅读苹果文档中了解到它应该如此。我的印象是,在引用图像时不需要包含设备修饰符,但我必须这样做。
总结一下,我现在正在使用
- 图片~iPhone.png
参考我在 IB 中的图像并以编程方式获取某些图像。我现在让 iOS 识别出我位于视网膜屏幕上并相应地加载 @2x 图像。因此 @2x 修饰符必须放在最后,而 ~iPhone 修饰符必须包含在“.png”的名称中。
这对我有用。希望它对其他人有帮助。请注意,我只为 iOS4.1 及更高版本构建应用程序,因此如果您支持以前的版本,可能会出现一些问题。
Ok...so after much experimenting I got it working.
I had two images named:
and I was trying to load them using IB or imageWithContentsOfFile using
This worked fine in the simulator but not on my device. I just got a blank white screen where the image should be.
I finally renamed the high resolution image to:
Moving the '@2x' modifier after the device modifier(~iPhone) when referencing my images allowed it to work the way I understood that it should from reading Apple's docs. I was under the impression that you didn't need to include the device modifier when referencing images but I had to.
To sum up, I am now using
- image~iPhone.png
to reference my images in IB and programatically for some images. I now get iOS recognizing that I am on a retina screen and loading the @2x images accordingly. So the @2x modifier had to go at the end and the ~iPhone modifier had to be included in the name of the '.png'.
That is what worked for me. Hope it helps someone else. Note that I am only building my app for iOS4.1 and above so there might be some issues with this if you are supporting previous version.
iOS 不会像这样自动为设备选择正确的图像。您必须编写代码来检查它是哪个设备,并按全名设置图像。
例如
if ([[UIScreen mainScreen] scale] == 2) // set hi res image
或者,您可以在两者中使用相同的图像,并将内容模式设置为缩放以填充。它看起来会一样。
编辑:尝试在文件名上写入
~iphone
(小写),或者根本不写入~iPhone
。如果你的应用程序不是通用的,那么编写~iphone
后缀是完全没有意义的。iOS does not automatically pick the right image for the device like that. You are going to have to write code to check which device it is, and set the image by full name.
e.g.
if ([[UIScreen mainScreen] scale] == 2) // set hi res image
Or, you can just use the same image in both, and set the content mode to scale to fill. It will look the same.
EDIT: Try writing either
~iphone
(lowercase), or just don't write~iPhone
at all on the file name. If your app is not universal, then writing the~iphone
suffix is completely pointless.iOS 文件系统区分大小写,设备修饰符应小写,
@2x 位于设备修饰符之前。
请参阅资源编程指南
iOS file system is case sensitive and device modifiers should be lowercase, it should be
The @2x comes before the device modifier.
See the resource programming guide