0

I have images in my project (which is only supported on iPhones, no iPads). The images are under a group and not under images.xcassets.

I want to use the same image for all iPhones, except the @3x for the 6 Plus, so I have a structure as follows:

 Project |_ Images_Group | |_ [email protected] |_ [email protected] 

I am using this code to use it in my app:

if let path = NSBundle.mainBundle().pathForResource("pic01", ofType: "png") { image = UIImage(contentsOfFile: path) } 

But path always returns nil. Do I need to have pic01.png?

The image is present under Project > Build Phases > Copy Bundle Resources. What am I doing wrong?

1 Answer 1

2

The tool you want here is UIImage(named:), which will look up images the way you're suggesting. pathForResource is a generic function that doesn't have special rules for images. It's looking specifically for pic01.png, which does not exist.

Sign up to request clarification or add additional context in comments.

4 Comments

I see. Somehow I missed that. I don't want the image cached, hence I was using pathForResource. I guess there is no way to work around it. Thanks for the answer!
Just out of curiosity, can you point to the developer documentation where this is laid out?
If you don't want it cached, you'll have to construct the path name by hand using the scale. I'm not aware of any method that will return the scale-specific path (which is slightly surprising, but true to the best of my knowledge).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.