setImageNamed: on a real device vs. the simulator

This may have been solved before but I add this note for posterity:


The simulator is able to find images whether they are loaded up to the watch or left on the device. Specifically, the command:

[self.myImage setImageNamed: @"fileName.png"

works fine in the simulator but on a device you get the error:

"Unable to find image named fileName.png"


They say you just need to load the image in the Images.xcassets or something - but I could not get that to work. (Actually, I got it to work for an image in the Glance but not for images in the InterfaceController - go figure!) I did get it to work by caching it as follows:


[[WKInterfaceDevice currentDevice] addCachedImage:[UIImage imageNamed:@"fileName.png"] name:@"myImageName"];

then

[self.myImage setImageNamed:@"myImageName"];

The setImageNamed: API requires that the image files be in the Watch App target (not the extension target). That may explain the inconsistent behavior.

> "Unable to find image named fileName.png"

Know that the simulator cares not about case-sensitivity.

On a device, however....


     fileName.png


...is not the same thing as...

     filename.png

Be careful to match code with case in all cases. If you post-juggle a file name to accomplish this, be sure to option-clean the build to encourage Xcode to forget cached references, etc.

Yes it explains the behavior. What is inconsistent is how you get an image file onto the Watch App target not teh WatchKit Extension - I can't seem to do that reliably for all images. Also inconsistent is the requirement that image files be on the Watch App target - the simulator doesn't seem to care OR ELSE when run from the simulator the images get to the Watch App target. but when run on a real device they do not.

Thanks for the suggestions but no, there is no problem with uppercase/lowercase filenames nor does an option-command-shift-k (option clean, clean build folders) change anything. For some reason I can't get these images to be loaded as part of the Watch App and so I need to upload them through addCachedImage: . Interestingly, the images are available (i.e. without needing to addCachedImage) to the GlanceController but not the InterfaceController.

Hmm, which "Images.xcassets" is the "myImageName" stored? It should be in the watch app one, not the extenstion and not the main iOS app.

It was in an Images.xcassets file that was listed in the column on the left of Xcode under the Supporting Files folder under the WatchKit App. In the Finder it was in the Images.xcassets folder that was in the WatchKit App folder. Also included in this folder is icons of size 48, 55, 58, 80, 87, 88, 172, and 196; 172, 344 and 516;

Good info - thanks for the followup.

setImageNamed: on a real device vs. the simulator
 
 
Q