UIImage imageNamed返回nil [英] UIImage imageNamed returns nil

查看:747
本文介绍了UIImage imageNamed返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS和Xcode的新手。

I am pretty new to iOS and Xcode.

我试图加载图片

[UIImage imageNamed:@"imageName.png/jpg and so on"];

但它只返回nil。图像应该在我的项目包中,考虑到我可以从InterfaceBuilder中的拖放菜单中选择它,并通过添加文件到项目名...菜单将其添加到Xcode。

but it only returns nil. The image should be in my project bundle, considering the fact that I can choose it from drag and drop menus in the InterfaceBuilder and it was added to Xcode via the "Add files to "projectname"..." menu.

我使用.png或.jpg图像没有区别,结果保持不变:它们通过IB工作,但如果我尝试自己运行imageNamed方法则不行。

It makes no difference whether I use .png or .jpg images, the result stays the same: they work via IB but not if I try to run the imageNamed method myself.

推荐答案

只有几个原因可以使用imageNamed来恢复图像:

There are only a few reasons why an image would come back nil with imageNamed:


  • 图片不在您的包中。确保图像实际上在您的项目中。确保通过单击文件并选择其所属的目标来检查目标。

  • The image is not in your bundle. Make sure the image is actually in your project. Make sure the target is checked by clicking on the file and selecting the target it belongs to.

您的图像名称拼写错误或扩展程序有问题。

You have the image name spelled incorrectly or a problem with the extension.

您正在使用视网膜显示器,但没有@ 2x图像。尝试将模拟器更改为视网膜,看看它是否显示出来。

You are using a retina display but do not have an @2x image. Try changing your simulator to retina and see if it shows up.

一些提示:

如果您正在测试使用模拟器从模拟器中删除应用程序并清理项目,然后重新运行。如果它仍然出现,它应该显示在你的手机上(如果它不是它可能是文件名或@ 2x版本的问题)。

If you are testing using simulator delete the app off of your simulator and clean your project, then re-run. If it still shows up it should show up on your phone (if it doesn't it's probably an issue with the case of the filename or the @2x version).

如果您在手机上进行测试并且没有显示,请确保您使用的是相同版本的模拟器(如果您有iPhone 4 / 4s,请务必使用带视网膜的4 / 4s模拟器)。

If you are testing on your phone and it doesn't show up, make sure you are using the same version of the simulator (if you have an iPhone 4/4s make sure to use the 4/4s simulator with retina).

根据这篇文章最后一件事: JPG图像无法加载UIImage imageNamed 使用imageNamed并且没有扩展名的某些JPG类型存在一些问题。 IMO,你应该使用PNG,因为iOS会为你压缩,除非你只需要使用JPG。

One last thing according to this post: JPG image doesn't load with UIImage imageNamed There is some issue with certain JPG types working with imageNamed and no extension. IMO, you should be using PNGs anyway since iOS compresses them for you, unless you just have to use JPG.

这篇关于UIImage imageNamed返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆