345

I have an app where the user can choose an image either from the built-in app images or from the iphone photo library. I use an object Occasion that has an NSString property to save the imagePath.

Now in the case of the built-in app images I do get the file name as an NSString an save in the [occasion imagePath]. But in the 2nd case where the user picks an image form the photo library I get an NSURL which I want to convert to an NSString to be able to save it in [occasion imagePath].

Is it possible to convert the NSURL to an NSString?

beryllium
  • 29,669
  • 15
  • 106
  • 125
Ali
  • 4,205
  • 4
  • 25
  • 40

7 Answers7

705

In Objective-C:

NSString *myString = myURL.absoluteString;

In Swift:

var myString = myURL.absoluteString

More info in the docs:

Yash Jadhav
  • 95
  • 1
  • 14
Randall
  • 14,691
  • 7
  • 40
  • 60
136

If you're interested in the pure string:

[myUrl absoluteString];

If you're interested in the path represented by the URL (and to be used with NSFileManager methods for example):

[myUrl path];
Venk
  • 5,949
  • 9
  • 41
  • 52
viggio24
  • 12,316
  • 5
  • 41
  • 34
  • Hi, Thanx for your answer. As I'm trying to load the image from the straing I saved, I'm really interested in what you mentioned about using NSFileManager with `[myUril path];` When I used path instead of absoluteString it gave me the name `asset.jpg` Could you pleas eelaborate more how to use this to load the image into a UIImage instance? – Ali Nov 13 '11 at 15:17
  • Hi. Of course from the file name to get a real file system path you need to know where the file is stored and then create the full path (directory + filename) by composing them using NSString's stringByAppendingPathComponent: .The only exception to this rule is when you know the image is stored in the app bundle, in this case you can just use the [UIImage imageNamed:"name"] call which does the full job for you. But in the other cases you have to specify your full path. – viggio24 Nov 14 '11 at 09:18
  • Hi, thanx again for your answer. I'm still confused on how to load the image to a UIImage using this NSString `assets-library://asset/asset.JPG?id=1000000001&ext=JPG` which I saved by converting the result of `[myUrl absoluteString]` after converting it to an NSString. Here I made this question on this regard but got no answer. I'd appreciate it if you can answer it [http://stackoverflow.com/questions/8085267/load-an-image-to-uiimage-from-a-file-path-to-the-asset-library/8086096#8086096] – Ali Nov 15 '11 at 13:47
  • 2
    yes, we should use myUrl.path instead of myUrl.absoluteString when you want to use it with NSFileManager, thanks Viggo24!! – flypig Jul 18 '12 at 15:09
  • Since you have an `assets-library` URL, rather than a `file` one, it's incompatible with `NSFileManager`. Access to such URLs is controlled strictly by `ALAssetsLibrary`. – Mike Abdullah May 29 '13 at 08:38
  • This is the more accurate answer, since it displays the different string representations of the NSURL. There are times, like working with NSFileManager, where the absoluteString is not helpful. – siannopollo Feb 09 '14 at 20:39
  • What happens if NSURL is nil. Don't you get _fatal error: unexpectedly found nil while unwrapping an Optional value_ – Hope Nov 13 '15 at 18:46
  • In Objective-C there are no Optional values. All messages sent to nil return 0. – viggio24 Nov 13 '15 at 21:59
47

Try this in Swift :

var urlString = myUrl.absoluteString

Objective-C:

NSString *urlString = [myURL absoluteString];
beryllium
  • 29,669
  • 15
  • 106
  • 125
  • I used NSString *urlString = [myURL absoluteString]; But I had met this error and xcode was crashed. -[__NSCFString absoluteString]: unrecognized selector sent to instance 0x791a18e0 – Võ Mai Trinh Jun 22 '15 at 09:39
  • @VõMaiTrinh that means your `myURL` is an object of NSString class. As NSString doesn't have `absoluteString` method then it leads to a crash. – beryllium Jun 22 '15 at 09:45
17

Swift update:

var myUrlStr : String = myUrl.absoluteString
kmiklas
  • 13,085
  • 22
  • 67
  • 103
1

I just fought with this very thing and this update didn't work.

This eventually did in Swift:

let myUrlStr : String = myUrl!.relativePath!
Speedz
  • 151
  • 9
1

You can use any one way

NSString *string=[NSString stringWithFormat:@"%@",url1];

or

NSString *str=[url1 absoluteString];

NSLog(@"string :: %@",string);

string :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAAA1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif

NSLog(@"str :: %@", str);

str :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAA-A1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif

Shaik Thuphel
  • 75
  • 2
  • 10
-1

In Swift :- var str_url = yourUrl.absoluteString

It will result a url in string.

  • Duplicate of [answer provided 4 years prior](https://stackoverflow.com/a/24249988/1549818) – grg Nov 06 '20 at 19:43