How get URL from image

How get URL from image located in asset catalog


before use:

let path = Bundle.main.path(forResource: "***", ofType: "png")

or

let path = Bundle.main.path(forResource: "***", ofType: "png", inDirectory: "Assets.xcassets")

let urlI = URL(fileURLWithPath: path!)


but this cause error URL is nil


Help me please

Post not yet marked as solved Up vote post of FrankP2138 Down vote post of FrankP2138
17k views

Replies

if the image is in asset catalog, simply get it with :

let myImage = NSImage(named: NSImage.Name(rawValue: "***")

I want the url of the image , locate in asset catalog

I have the following problem:

I have translated a project from XCode 8 to Xcode 9

when I try to get an image URL with Bunble.

let path = Bundle.main.path(forResource: "***", ofType: "png")

let urlI = URL(fileURLWithPath: path!)

url result nil ? Why ????


the same code used on a new project created with Xcode 9

works well.

I have the following problem:

I have translated a project from XCode 8 to Xcode 9

when I try to get an Url of the with Bunble.

let path = Bundle.main.path(forResource: "***", ofType: "png")

let urlI = URL(fileURLWithPath: path!)

url result nil ? Why ????


the same code used on a new project created with Xcode 9

works well.

That should work, if the name is correct.


What do you have exactly (including uppercase) as "***" in the xcassets ? And what do you put in code ?

The String you pass as parameter must match exactly the one in xcassets.

For instance FirstImage and Firstimage will not be OK.

If you get nil, it is because the resource was not found.

Just in case someone wants to use a Data Set from an XCAsset, your should use NSDataAsset for loading it: https://stackoverflow.com/a/33370596/499113