UIImage(named: )returns nil during IB rendering

Discussion in 'iOS Programming' started by moonman239, Aug 9, 2016.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    I'm trying to render an image in the background of a UIView in Interface Builder. For some reason, however, the UIImage object is nil. I've checked my Build Settings to make sure the picture it represents is copied into the target, and the image file appears to be in the same directory & group as my source code.

    Here's my code:

    var image = UIImage(named: "string")
        override func prepareForInterfaceBuilder() {
            image!.drawInRect(self.bounds) // breakpoint here, where I see that image is nil
  2. Dookieman, Aug 9, 2016
    Last edited: Aug 9, 2016

    Dookieman macrumors 6502

    Oct 12, 2009
    Are you using xcassets or just adding the image directly to an image folder? Are you sure that image name is spelled correctly?
  3. AxoNeuron macrumors 65816


    Apr 22, 2012
    The Left Coast
    If the image is in xcassets then you should be able to just use the filename.

    But if you just copied the image itself to the project directory, then you should also include the file extension (ie. .jpg). You also need too make sure that the image (or the xcassets file) has the right target membership. You do this by clicking the file itself in the navigator, then on the right navigation bar click the 'file inspector' icon (the paper icon) at the top right. Usually, the first target is the iOS target but yours may be different.

Share This Page