ios UIImage imageNamed 返回 nil

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10998544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:44:42  来源:igfitidea点击:

UIImage imageNamed returns nil

iosxcodeuiimageimagenamed

提问by arne somborn

I am pretty new to iOS and Xcode.

我对 iOS 和 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.

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

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.

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

回答by Matt Hudson

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

图像返回 nil 并带有 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.

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

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

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

  • 如果您使用的是资产目录,请确保在属性检查器中选择了正确的设备。

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

如果您正在使用模拟器进行测试,请从模拟器中删除应用程序并清理您的项目,然后重新运行。如果仍显示它应该显示您的手机上(如果没有它可能与文件名的大小写或者@ 2个版本的问题)。

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

如果您在手机上进行测试但它没有出现,请确保您使用的是相同版本的模拟器(如果您有 iPhone 4/4s,请确保使用带有视网膜的 4/4s 模拟器)。

One last thing according to this post: JPG image doesn't load with UIImage imageNamedThere 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.

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

回答by Damo

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

回复 Mike Weller 的评论。复选框是......

enter image description here

在此处输入图片说明

回答by Kof

In my case, the PNG was malformed.

就我而言,PNG 格式不正确。

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

出于某种原因,Xcode 预览显示正确,但是当我尝试使用 UIImage 加载它时,它返回 nil。

回答by Travis M.

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")从您正在构建的动态框架内的代码返回 nil ,那么您必须将代码更改为:

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

This should retrieve the asset from CXAssets correctly.

这应该正确地从 CXAssets 检索资产。

回答by clocksmith

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

我刚刚在磁盘“file.jpg”上命名的 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!

我试图在没有扩展名的情况下加载,只是@"file"。虽然这适用于 png,但不适用于 jpg。一旦我使用@"file.jpg",它就起作用了!

回答by Kage

Try re-exporting your image.

尝试重新导出您的图像。

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

我有一个类似的问题, UIImage imageNamed 返回 nil 并且这些答案都没有解决我的问题。

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.

我发现实际上 png 文件有问题。我在 GIMP 图像编辑器中打开了该文件,将其另存为一个新文件,再次将其导出为 png 并神奇地开始工作。

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

我根本没有更改代码中的任何内容,因此实际图像文件肯定有问题。

回答by emrcftci

Swift 5

斯威夫特 5

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

如果你在动态框架中得到 UIImage nil,你应该使用 bundle identifier 设置图像。

You can create an extension to Bundle like this;

您可以像这样创建 Bundle 的扩展;

  • Create an extension;

    extension Bundle {
        public static let myFramework = Bundle(identifier: "com.myFramework.bundle")
    }
    
  • Then you can use like this;

    UIImage(named: "myImageName", in: Bundle.myFramework, compatibleWith: nil)
    
  • 创建扩展;

    extension Bundle {
        public static let myFramework = Bundle(identifier: "com.myFramework.bundle")
    }
    
  • 然后你可以这样使用;

    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 more useful because it's more readable than second one.

我认为第一个更有用,因为它比第二个更具可读性。

回答by iHux

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!

我有一个类似的问题,最后清理修复了它:图像显示在模拟器中,但在 iPhone 上运行应用程序时没有显示 id。
我做了什么:1)从 iPhone 中删除了应用程序 2)在修复了错误并显示图像之后执行了 Product/Clean!

回答by Jim

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.

我有同样的问题:@"photo.jpg" 导致 'nil' UIImage。更改为“实际”文件规范,@"photo.JPG" 工作正常!我没有意识到有大小写有!非常不直观。

回答by iMx

try

尝试

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

instead (with an extension)

相反(带扩展名)