xcode 图像未显示在界面生成器/iPhone 的 UIImageView 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2650364/
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-09-09 03:21:13  来源:igfitidea点击:

Image not showing in UIImageView in Interface Builder / iPhone

iphonexcodeinterface-builderuiimageview

提问by dbonneville

I have a UIViewwith an UIImageViewdragged onto the view. All of a sudden, for all my xibs, the image no longer shows up. There is a blue X. However, when it builds, the image is there.

我有一个UIView带有UIImageView拖动到视图。突然之间,对于我所有的 xib,图像不再出现。有一个蓝色的 X。但是,当它构建时,图像就在那里。

At one point, I deleted and regenerated all my images and moved some into a subfolder in Xcode. Normally, when you go to select an image for an UIImageView, IB allows you to pick from any image in the project. But, I can't see any of the images I had put in the folder anymore in the dropdown.

有一次,我删除并重新生成了所有图像,并将一些图像移到了 Xcode 的子文件夹中。通常,当您为 选择图像时UIImageView,IB 允许您从项目中的任何图像中进行选择。但是,我在下拉列表中再也看不到我放在文件夹中的任何图像。

All I see in the dropdown on the Inspector is the one image I want, but that is also the one that is not showing up. And like I said, if I build it on the device or simulator, it all works.

我在 Inspector 的下拉菜单中看到的只是我想要的一张图片,但那也是一张没有出现的图片。就像我说的,如果我在设备或模拟器上构建它,一切都会正常。

There is some cache or something screwed up somewhere. Everything builds with no errors. I cleared the caches and rebuilt. It all works. No error or warnings. But...I can't see any other images and IB still thinks it's missing the image that is clearly selected in the dropdown.

有一些缓存或某处搞砸了。一切都建立起来没有错误。我清除了缓存并重建。这一切都有效。没有错误或警告。但是...我看不到任何其他图像,IB 仍然认为它缺少在下拉列表中明确选择的图像。

So how do I get Xcode and IB back on track and see what assets it properly should be seeing in the XIBs?

那么我如何让 Xcode 和 IB 回到正轨并查看它应该在 XIB 中正确看到哪些资产?

采纳答案by Mihir Mehta

Try cleaning and rebuilding the project.

尝试清理和重建项目。

If it's still not working then remove all images from Xcode (select delete reference only) and then re-add them again by dragging those images into Xcode. Then re-build it and it should work.

如果它仍然不起作用,则从 Xcode 中删除所有图像(选择仅删除引用),然后通过将这些图像拖到 Xcode 中重新添加它们。然后重新构建它,它应该可以工作。

回答by user1949873

My recipé: CLEAN + Build + QUIT XCODE AND THEN REOPEN IT!

我的方法是:清理 + 构建 + 退出 XCODE,然后重新打开它!

Voilá!! It's there!

瞧!!在那!

回答by William Cerniuk

Make sure you have added your image to target when dragging it into the project. Because Xcode's folder / group / project system is buggy, click on the image and check that it is actually added to the target build in the inspector as shown (a graphical problem deserves a graphic answer!) :

将图像拖入项目时,请确保已将图像添加到目标。因为 Xcode 的文件夹/组/项目系统有问题,单击图像并检查它是否确实添加到检查器中的目标构建中,如图所示(图形问题应该得到图形答案!):

Add the image to the project...

将图像添加到项目...

enter image description here

在此处输入图片说明

Add to targets... (or so we think)

添加到目标...(或者我们认为)

enter image description here

在此处输入图片说明

Background image shows nicely in Interface Builder...

背景图像在界面生成器中很好地显示...

enter image description here

在此处输入图片说明

But when we run, we have nothing but the background color we set... no image...

但是当我们运行时,除了我们设置的背景颜色之外什么都没有……没有图像……

enter image description here

在此处输入图片说明

Clicking on the UIImageView in Interface Builder and using the inspector, we scroll down and find that in fact it was never added to the target like Xcode clearly showed in step 2...

单击 Interface Builder 中的 UIImageView 并使用检查器,我们向下滚动,发现实际上它从未像第 2 步中清楚显示的 Xcode 那样添加到目标中......

enter image description here

在此处输入图片说明

So we add it to the target...

所以我们将它添加到目标...

enter image description here

在此处输入图片说明

And we build and run and voila! Our image shows in the UIImageView exactly where it is suppose to show.

我们构建并运行,瞧!我们的图像在 UIImageView 中准确显示了它应该显示的位置。

enter image description here

在此处输入图片说明

This drove me nuts for the better part of the day until I tried adding code to make it happen to see if it was a flakey image. I added it without issue by creating the the viewDidLoad implementation below.

这让我在一天中的大部分时间都发疯了,直到我尝试添加代码来让它发生,看看它是否是一个古怪的图像。我通过创建下面的 viewDidLoad 实现毫无问题地添加了它。

Note that you don't need this if Xcode / Interface Builder is working the way it should. Either user IB or use this code, not both :

请注意,如果 Xcode/Interface Builder 按应有的方式工作,则不需要它。用户 IB 或使用此代码,而不是两者:

- (void)viewDidLoad {
UIColor *   theColor;
UIImage *   theImage;

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
theImage = [UIImage imageNamed:@"/<redacted>/background sea green gradent.png"];
theColor = [UIColor colorWithPatternImage: theImage ];
[self.view setBackgroundColor: theColor ]; }

Note: This can be the same issue if your button is not showing a custom image...

注意:如果您的按钮未显示自定义图像,这可能是相同的问题...

回答by Cyberpass

Just ran into this issue. Exported the identical images in Adobe Illustrator. One as transparent, the other as white. The transparent one showed up with a big blue question mark. Running it, worked fine however. Something to do with IB and transparent PNGs

刚遇到这个问题。在 Adob​​e Illustrator 中导出相同的图像。一个是透明的,另一个是白色的。透明的出现了一个蓝色的大问号。运行它,但是工作正常。与IB和透明PNG有关

回答by longi

just for the record (because nothing worked for me):

仅作记录(因为对我没有任何作用):

We only had retina-graphics in our projects. After update to XCode 5all images had a white question mark in it (only in the Interface Builder, not in Simulator). We switched the image source to non-retina, which solved the case!

我们的项目中只有视网膜图形。更新XCode 5所有图像后,其中有一个白色问号(仅在 中Interface Builder,不在 中Simulator)。我们将图像源切换到non-retina,解决了这个问题!

enter image description here

在此处输入图片说明

Using btn-category-6.jpgsolved the issue!

使用btn-category-6.jpg解决了问题!

回答by kevnm67

For me it was because the images had a ".png" in attributes inspector. Removing it fixed the issue

对我来说,这是因为图像在属性检查器中有一个“.png”。删除它解决了问题

回答by Manab Kumar Mal

In my case when I have created new project in Xcode 6I can not show the images in the Simulator and as well as in device. I have imported all the images in the image assets making a folder within that.Now I have imported all the images, all are working fine now. All images are now showing now.

在我的情况下,当我在中创建新项目时,Xcode 6我无法在模拟器和设备中显示图像。我已经导入了图像资产中的所有图像,并在其中创建了一个文件夹。现在我已经导入了所有图像,现在一切正常。现在显示所有图像。

Follow these steps:

按着这些次序:

STEP 1:

第1步:

Step 1:

第1步:

STEP 2:

第2步:

Step 2

第2步

STEP 3:

第 3 步:

Step 3

第 3 步

and then Import images which you need. Reference Link: StackOverflow link

然后导入您需要的图像。参考链接: StackOverflow 链接

回答by whaleshark

Just an FYI: I had a similar problem. At some point the images were corrupted and they were all Transparent PNGs!

仅供参考:我遇到了类似的问题。在某些时候,图像已损坏,它们都是透明的 PNG!

I replaced the files and now they are showing again. The image corruption may have happened when I copied the files via Windows Explorer in Oracle's Virtual Box running Windows XP.

我替换了文件,现在它们再次显示。当我通过运行 Windows XP 的 Oracle Virtual Box 中的 Windows 资源管理器复制文件时,可能发生了图像损坏。

回答by Tanhan

This happened when I moved the images to xcassets. I solved it by removing png and jpg extensions on inspector.

当我将图像移动到 xcassets 时发生了这种情况。我通过删除检查器上的 png 和 jpg 扩展来解决它。

回答by oberbaum

I had the same symptoms, but I just moved the project folder to the desktop, re-ran the project in IB from there and it worked! Then work it back into the same parent folder again, with a different name...strange.

我有同样的症状,但我只是将项目文件夹移动到桌面,从那里重新运行 IB 中的项目并且它工作了!然后再次将它放回到同一个父文件夹中,使用不同的名称......奇怪。