将 NSURL 转换为 NSString

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?

转载于:https://stackoverflow.com/questions/8082719/convert-an-nsurl-to-an-nsstring

7个回答

NSString *myString = myURL.absoluteString;

More info in the docs:

csdnceshi66
必承其重 | 欲带皇冠 probably because of its poor naming
6 年多之前 回复
csdnceshi52
妄徒之命 Thanx. I dunno how I missed it when I read the docs.
大约 8 年之前 回复

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];
csdnceshi72
谁还没个明天 In Objective-C there are no Optional values. All messages sent to nil return 0.
大约 4 年之前 回复
weixin_41568184
叼花硬汉 What happens if NSURL is nil. Don't you get fatal error: unexpectedly found nil while unwrapping an Optional value
大约 4 年之前 回复
csdnceshi55
~Onlooker 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.
接近 6 年之前 回复
csdnceshi57
perhaps? 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.
6 年多之前 回复
csdnceshi63
elliott.david yes, we should use myUrl.path instead of myUrl.absoluteString when you want to use it with NSFileManager, thanks Viggo24!!
7 年多之前 回复
weixin_41568183
零零乙 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 [stackoverflow.com/questions/8085267/…
大约 8 年之前 回复
csdnceshi72
谁还没个明天 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.
大约 8 年之前 回复
weixin_41568183
零零乙 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?
大约 8 年之前 回复

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

This eventually did in Swift:

let myUrlStr : String = myUrl!.relativePath!

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

Swift update:

var myUrlStr : String = myUrl.absoluteString
weixin_41568184
叼花硬汉 You actually should omit String - the Swift Design Guidelines try to achieve as little redundant code as possible.
3 年多之前 回复
csdnceshi75
衫裤跑路 You could omit String since Swift uses type inference.
5 年多之前 回复

Try this in Swift :

var urlString = myUrl.absoluteString

Objective-C:

NSString *urlString = [myURL absoluteString];
csdnceshi75
衫裤跑路 that means your myURL is an object of NSString class. As NSString doesn't have absoluteString method then it leads to a crash.
4 年多之前 回复
csdnceshi52
妄徒之命 I used NSString *urlString = [myURL absoluteString]; But I had met this error and xcode was crashed. -[__NSCFString absoluteString]: unrecognized selector sent to instance 0x791a18e0
4 年多之前 回复

In Swift :- var str_url = yourUrl.absoluteString

It will result a url in string.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!