1

I am trying to load a .PNG image located in my XCode project as UIImage - but the constructor of UIImage that takes file name returns nil.

Please see the screenshot for my setup:

enter image description here

Here are some details:

  • The project is in Swift
  • I am constructing the UIImage in global variable (lazyly)
  • The issue is pressent in simulator and on the device (iPhone 5S)
  • I have added same image several times with different names to make sure incorrect naming is not the cause (see all the names in left red rectange)
  • all images are added to EasyTypeShared target, which is a library project embedded into runeable EasyType project.
  • I did not do any axtra steps - just added the image files to my workspace (and the EasyTypeShared target) and run the app

EDIT: According to this blog the cause might be that workspace setup - more preciselly that the image is being loaded by framework. Not sure but in lack of better ideas I will follow this track. If you can cast some light on the matter I would by really thankful for any ideas.

Rasto
  • 17,204
  • 47
  • 154
  • 245
  • 1
    Just because you can see the file in Xcode does not mean it's in your bundle. – Hot Licks Feb 05 '15 at 20:34
  • @HotLicks What do I have to do to add it to the bundle? I thought that with this particular `UIImage` constructor there is no additional step (like addin it to the bundle) required. – Rasto Feb 05 '15 at 20:39
  • Select the image and make sure the image has the "Target Membership" selected in the File inspector. – augustzf Feb 05 '15 at 21:39
  • just add an Asset Catalog and put your images there – longbow Feb 05 '15 at 22:46
  • As shown in the picture you have some strange names for the image files. You need at least: ImagePlaceholder.png and ImagePlaceholder@2x.png (and possibly ImagePlaceholder@3x.png, if you are planning to use iPhone 6Plus). Make sure that the files are not in a real folder (is Libraries a real folder?) as opposed to just a grouping which also shows up as a folder in the file explorer – Syed Tariq Feb 06 '15 at 03:53

3 Answers3

2

The problem was caused by the fact that code loading the image from resources resides in framework. Main bundle does not contain resources for the framework, the corresponding bundle needs to be used. Working code:

private let imagePlaceholder = UIImage(named: "ImagePlaceholder.png", inBundle: NSBundle(forClass: CarretMoveController.self)
Rasto
  • 17,204
  • 47
  • 154
  • 245
1

Are these part of the images.xcassets? If not they will have to be read using NSBundle and getting a path/URL for the resource and all that and a simple filename will not suffice.

You can add the files to the assets as per the Apple documentation here. Relevant section might be the 'Adding Image Sets' one.

If you do not have images.xcassets, you can add it to your project as per one of the answers given here.

Community
  • 1
  • 1
codingminion
  • 401
  • 4
  • 7
0

in your project explorer I can see that images are not named properly. it should be like this:

imagePlaceHolder.png
imagePlaceHolder@2x.png

notice the png at the end of the file name, and @2x before the file extension.

Rasto
  • 17,204
  • 47
  • 154
  • 245
Hashmat Khalil
  • 1,826
  • 1
  • 25
  • 49
  • Just renaming to `ImagePlaceholder@2x.png` does not seems to work. @HotLicks wrote something about adding it to bunde. Maybe that is the issue? – Rasto Feb 05 '15 at 20:46