UIImage imageNamed returns nil

2020-01-24 23:08发布

I am pretty new to iOS and Xcode.

I tried to load an image with

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

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.

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.

19条回答
ら.Afraid
2楼-- · 2020-01-24 23:15

Does the image work on the simulator but not on the device?

If so, in this scenario it is always to do with the case of the name. The device requires exact case and the simulator is not so fussy if I remember correctly.

e.g. for an image named image.png

[UIImage imageNamed:@"Image"];

would work on the simulator but not on the device...

查看更多
Ridiculous、
3楼-- · 2020-01-24 23:16

Try to use UIImage(contentsOfFile:) instead of imageNamed. It worked for me with downloaded images.

查看更多
Viruses.
4楼-- · 2020-01-24 23:17

try

[UIImage imageNamed:@"imageName.png"];

instead (with an extension)

查看更多
劫难
5楼-- · 2020-01-24 23:17

I had the same problem: @"photo.jpg" resulted in 'nil' UIImage. Changing to the "actual" filespec, @"photo.JPG" works fine! I hadn't realized there was case-sensitivity there! VERY non-intuitive.

查看更多
女痞
6楼-- · 2020-01-24 23:19

In my case problem was with image name in asset catalog. For example if you have image with name "TEst" and change name to "Test", changes won't be indexed. If your teammate will pull commit with this changes, image in his asset catalog will have previous name "TEst". Very small chance to catch this situation but it's possible.

查看更多
Root(大扎)
7楼-- · 2020-01-24 23:20

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.

  • 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.

  • If you are using an asset catalog make sure you have the correct devices selected in the attribute inspector.

Some tips:

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).

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).

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.

查看更多
登录 后发表回答