0

I have an imageView with an outlet called humanEyesUIIV and now simply want to add an image programmatically. I use the following code:

humanEyesArray = [
        UIImage(named: "humanEyes0")!
        ]

   humanEyesUIIV.image? = humanEyesArray[0]

No error shows and the simulator runs fine, but the picture doesn't show up for some reason. I have looked at this thread and the code there works fine:

How do you create a UIImage View Programmatically - Swift

let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)

imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(imageView)

The problem is just that I want to insert the imageView in the UI and handle its constraints and so on there, then simple add the image programatically. Any ideas what's wrong with my code?

Dennis Vennink
  • 1,083
  • 1
  • 7
  • 23
Joakim Sjöstedt
  • 824
  • 2
  • 9
  • 18
  • Have you checked if your image is `nil`? This is the behavior you'll see if `UIImage(named:"humanEyes0")!` is (a) syntactically correct but (b) the actual image name is misspelled or has the wrong file type or cannot be found. Try setting a breakpoint and before that check what the first element in your array is. NOTE: Your syntax looks good. –  Oct 21 '17 at 23:22
  • Also make sure your constraints are correct and the imageView has a frame. A simple way to check this might be to give the imageView a background color and check if it shows up. – Stefan Church Oct 21 '17 at 23:25
  • Although if you’re trying to use the size of the image to intrinsically size the imageView it may not have a size if there is a problem with the image. How have you added the imageView to the view hierarchy? – Stefan Church Oct 21 '17 at 23:38
  • Thank you for your replies :) I checked and the image did have a value (and the image did show when I tried using the code from the other thread). I also checked the imageView and it had a value. Gave it a bg color and it showed in the simulator. Still no image though :( I did a print() of the image and the image View and it came back: [, {280, 280}, > – Joakim Sjöstedt Oct 21 '17 at 23:43
  • Have you tried displaying another test image? Is the alpha 0.4 on purpose? – Stefan Church Oct 21 '17 at 23:56

2 Answers2

1

To get you going, just put a simple UIImageView in the scene in storyboard. Make it large, ensure you have four constraints, and put in an image of a cat or something.

Now add an @IBOutlet var test UIImageView! and connect it.

Next add some code like test.alpha = 0.1 just to absolutely check you are OK so far.

Finally, just go test.image = .. your image.

This will allow you to test one problem at a time.

Fattie
  • 27,874
  • 70
  • 431
  • 719
0

OK, solved it now by removing the question mark :) So, instead of:

   humanEyesUIIV.image? = humanEyesArray[0]

I now have:

   humanEyesUIIV.image = humanEyesArray[0]

That's it! :) Thanks for helping me out :)

Joakim Sjöstedt
  • 824
  • 2
  • 9
  • 18