Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the image of a UIImageView in Interface Builder

When ever I set an image for a UIImageView in IB and simulate the interface the buttons and stuff show up but the image view doesn't.

like image 219
Mark Szymanski Avatar asked Nov 14 '09 15:11

Mark Szymanski


People also ask

How do I display an image in SwiftUI?

You can display an image in a SwiftUI view by using the Image view. First you need to add the image to a new image set in your Assets. xcassets file in the Xcode project navigator.

How do I display an image in Xcode?

Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”. After that, drag and drop an image into the newly create Image Set, placing it at appropriate 1x, 2x or 3x slot.

What happens when you set an imageView image property to nil?

image property will automatically handle that for you (when the new image is assigned). Where setting to nil is useful is where you want to explicitly mark the image as no longer in use, but do not want to remove / delete the UIImageView itself or set a new image. In those cases, setting . image = nil is a good idea.

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


1 Answers

That's because the simulator used by Interface Builder is independent of your project. It's just taking your xib files, building the interface, and displaying it. As such, any resources it finds references to but can't find will be skipped. So if your UIImageView uses an image named "myImage.png", then when the simulator runs, it's going to try to execute [UIImage imageNamed:@"myImage.png"];. Since "myImage.png" is not part of the Simulator bundle, it will fail to create the image, and your UIImageView will be blank.

It can create buttons because (obviously) buttons are standard, globally available code. Your project-specific resources are not.

like image 173
Dave DeLong Avatar answered Sep 22 '22 17:09

Dave DeLong