tranvutuan
tranvutuan

Reputation: 6109

ways to get the path of specific file in ios

To localitie items in the standard directories, I have just used either one of these:

NSURL* url = [[NSBundle mainBundle] URLForResource:@"search" withExtension:@"png"]; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"search" ofType:@"png"];

The output is:

url  is file://localhost/Users/myName/Library/......./MyApp.app/search.png
path is                 /Users/myName/Library/......./MyApp.app/search.png

The difference between the two of them is the missing file://localhost.

My question is when should we use URLForResource and when we use pathForResource?

Upvotes: 1

Views: 1588

Answers (1)

tim
tim

Reputation: 1682

Most iOS methods offer you to use path or url, for example

[NSData dataWithContentsOfFile:foo]
[NSData dataWithContentsOfURL:foo]

So it doesn't really matter which one you use.

For methods that require a path you could do a conversion:

[UIImage imageWithContentsOfFile:yourFilePath]

becomes

[UIImage imageWithContentsOfFile:[NSURL fileURLWithPath:yourFilePath]]

You can also convert a URL to a Path

yourURL.path

Upvotes: 2

Related Questions