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
?
In Objective-C:
NSString *myString = myURL.absoluteString;
In Swift:
var myString = myURL.absoluteString
More info in the docs:
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];
Try this in Swift :
var urlString = myUrl.absoluteString
Objective-C:
NSString *urlString = [myURL absoluteString];
myURL
is an object of NSString class. As NSString doesn't have absoluteString
method then it leads to a crash.
Swift update:
var myUrlStr : String = myUrl.absoluteString
String
since Swift uses type inference.
String
- the Swift Design Guidelines try to achieve as little redundant code as possible.
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
In Swift :- var str_url = yourUrl.absoluteString
It will result a url in string.
Success story sharing
[myUril path];
When I used path instead of absoluteString it gave me the nameasset.jpg
Could you pleas eelaborate more how to use this to load the image into a UIImage instance?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/…assets-library
URL, rather than afile
one, it's incompatible withNSFileManager
. Access to such URLs is controlled strictly byALAssetsLibrary
.