UIImage imageNamed returns nil

早过忘川 提交于 2019-11-26 22:22:23
Matt Hudson

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.

Re Mike Weller's comment. The checkbox is ....

In my case, the PNG was malformed.

For some reason Xcode preview shown it correctly, but when I tried loading it with UIImage, it returned nil.

I just had this issue for jpg files named on disk "file.jpg"

I was trying to load without the extension, just @"file". While this works fine for pngs, it does not for jpg. Once I used @"file.jpg", it worked!

If you verified all of the above and are finding that your UIImage(named: "myImage") returns nil from code inside of a dynamic framework you are building, then you have to change the code to:

UIImage(named: "myImage", in: Bundle(identifier: "com.myframework.name"), compatibleWith: nil)

This should retrieve the asset from CXAssets correctly.

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.

Try re-exporting your image.

I had a similar problem, UIImage imageNamed was returning nil and none of these answers fixed my problem.

I found out that it was actually something wrong with the png file. I opened the file in GIMP image editor, saved it as a new file, exported it again as png and magically it started working.

I didn't change anything in code at all so there was definitely something wrong with the actual image file.

i had a similar issue, finally the cleanup fixed it: the image showed up in the simulator, but id did not show up when running the app on the iPhone.
What I did: 1) deleted the app from the iPhone 2) performed Product/Clean after that the bug was fixed and the image showed up!

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

Check that file has no space at the end. The name like name @2x.png will fail loading, but name@2x.png is fine

Swift 5

If you get UIImage nil in dynamic framework, you should set image with bundle identifier.

You can create extension for Bundle like this;

  • Create extension;

    extension Bundle { public static let myFramework = Bundle(identifier: "com.myFramework.bundle") }

  • Then use like this;

    UIImage(named: "myImageName", in: Bundle.myFramework, compatibleWith: nil)

Or

You can use like this;

  • UIImage(named: "myImageName", in: Bundle(for: type(of: self)), compatibleWith: nil)

I think the first one is useful for readable code.

Enjoy

try

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

instead (with an extension)

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

Make sure you have the image for the DEVICE you are using in your .xcassets file. An image flagged for iPad won't show up on the phone (or the simulated phone).

In addition to @Inturbidus's answer: For those who came here working on Application Extensions:

The image you are working with should belongs to both targets - hosted app and extension.

In other case you'll always getting nil trying to access it from within the extension's code.

For Xcode 6.4, click on Images.xcassets and check whether there are entries for each image that you called. You may right-click on the image and select "Show in Finder" to check if the images are the correct ones added.

Make sure the Attributes Inspector name field of the image matches exactly the name you're using. In my case the image was part of a folder and so the image name was "folder/name". Using this long name solved the problem. XCode 7.3.1

In my case, using a name with accented characters proved to be a poor idea. I changed Astéroïdes to Asteroids and it worked fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!