22

having trouble getting NSSTRING to convert to NSURL, item.image, holds the url for an image that im getting through xml

NSString *urlString = [NSString stringWithFormat:@"%@", item.image];
NSURL *url = [NSURL URLWithString:urlString];

NSLog(@"string> %@ ", urlString);
NSLog(@"url> %@ ", url);

2011-06-23 11:18:49.610 Test[10126:207] string> http://www.harlemfur.com/images/Dog_Olive.jpg       
2011-06-23 11:18:49.611 Test[10126:207] url> (null) 

also if i try :

NSString *urlString = [NSString stringWithFormat:@"%@", item.image];
NSURL *url = [NSURL fileURLWithPath :urlString];

2011-06-23 11:22:08.063 Test[10199:207] string> http://www.harlemfur.com/images/Dog_Olive.jpg

2011-06-23 11:22:08.064 Test[10199:207] url> %0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://www.harlemfur.com/images/Dog_Olive.jpg%0A%20%20%20%20%20%20%20%20%20%20%20%20 -- / 
user616860
  • 309
  • 1
  • 3
  • 10
  • Cannot reproduce. The problem must be with item.image. What type is it exactly? – Eiko Jun 23 '11 at 15:46
  • Do these lines appear in your code just like that? Directly one after another (constructing and logging)? – Eiko Jun 23 '11 at 15:49

4 Answers4

80

When making URL from NSString, don't forget to encode it first, so try this:

NSString *urlString = [NSString stringWithFormat:@"%@", item.image];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];  

For IOS ≥ 9.0 use

NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
Mihir Oza
  • 2,768
  • 3
  • 35
  • 61
cxa
  • 4,238
  • 26
  • 40
  • thanks but this is the print out im getting :2011-06-23 11:41:34.796 Test[10609:207] string> http://www.harlemfur.com/images/Dog_Olive.jpg 2011-06-23 11:41:34.797 Test[10609:207] url> %0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://www.harlemfur.com/images/Dog_Olive.jpg%0A%20%20%20%20%20%20%20%20%20%20%20%20 – user616860 Jun 23 '11 at 15:42
  • Then you should check your item.image, make sure there is no hidden character. Checkout NSString documentation to see how to trim string. – cxa Jun 23 '11 at 15:45
  • you were right, i had extra spaces in my xml, thanks dude, works like a charm – user616860 Jun 23 '11 at 15:51
  • 1
    Just a quick update, stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding is deprecated for IOS ≥ 9.0. You should use: stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet] – Laurenswuyts Oct 10 '16 at 13:46
2

All is ok, if you want to get the URL to print in NSLog use this:

NSLog(@"url> %@ ", [url absoluteString]);
Cyprian
  • 9,423
  • 4
  • 39
  • 73
  • 2
    You *can* print objects directly in Objective-C. It calls the `description` method. Works fine with NSURL. – Eiko Jun 23 '11 at 15:45
1

item.image does not only contain the URL, but it starts with a newline and spaces. Remove those first and you should be fine.

Eiko
  • 25,601
  • 15
  • 56
  • 71
0

In Swift:

NSURL(fileURLWithPath: item.image!)
Rutger Huijsmans
  • 2,330
  • 2
  • 30
  • 67