简体   繁体   中英

How to set UIImageView programmatically in XCode?

Hi so I'm looking to fill my UIImageView with an image that I have in my files, and I don't want a specific one all the time, but a different one every other time.

That is why I created an array with the names of all the images that I would like to use.

The only problem is there is always this error, exc_bad_instruction (code=exc_i386_invop subcode=0x0) , that constantly comes up everytime I try to run the code. Here is my code,

please help: 在此处输入图片说明 Also I am running xcode version 7 if that makes any difference.

Your UIImageView is nil and it can't be if you are creating it. You have to set the image BEFORE you create the view. The image should already be ready and waiting when the view is created.

The image ( image1 ) view you are trying to set image is nil. You have to first create the image view, and then assign image.

Try this code:

image1 = UIImageView()
//Set up frame, size, etc
image1.image = UIImage(named: imageName)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM