Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you build an NSURL for an image in Image.xcassets?

The easiest way to load a local UIImage asset is of course to use +[UIImage imageNamed] but for my case, I want to supply a NSURL *. How do I get a NSURL * for local file?

like image 216
fatuhoku Avatar asked Apr 24 '15 19:04

fatuhoku


1 Answers

While using xcasset, Xcode put all your files in a new one. 1 file to manage them all. So, you can't access to an image store inside an xcasset directly. From apple's documentation:

For projects with a deployment target of at least iOS 7 or OS X 10.9, Xcode compiles your asset catalogs into a runtime binary file format that reduces the download time for your app.

If you have to access to the file, you have two choice:

  • Don't use the xcasset for this image, but put your image as an normal image in your project and use the NSBundle to get it
  • Use UIImage imageNamed, and save it in the directory of your choice

But whatever your need, you should think another way to manage it. The above solutions will work, but I'm sure you can have a nicer solution.

like image 200
tbaranes Avatar answered Nov 05 '22 23:11

tbaranes