Image not showing in UIImageView in Interface Buil

2019-02-11 12:30发布

I have a UIView with an UIImageView dragged 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.

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.

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.

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.

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

18条回答
forever°为你锁心
2楼-- · 2019-02-11 13:09

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

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.

查看更多
We Are One
3楼-- · 2019-02-11 13:09

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.

查看更多
太酷不给撩
4楼-- · 2019-02-11 13:10

If you have all of the 1x, 2x, and 3x images set, cleaned and built the project and it still doesn't show up correctly, exit Xcode and force quit, then open Xcode again. Just a simple Xcode restart worked for me.

查看更多
混吃等死
5楼-- · 2019-02-11 13:11

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

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-11 13:12

For me the problem was, I could see the new added image files on Show the media library but not in the dropdown for my button Image

For me the solution was, typing the name manually. When I started typing the image names in the text box, it autocompleted it and that's it. |

PS: I also implemented the selected solution before doing so

enter image description here

查看更多
家丑人穷心不美
7楼-- · 2019-02-11 13:13

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!) :

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

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.

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.

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 :

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

查看更多
登录 后发表回答