0
_profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@".../images/Profile/Pratik Maniya Photo.jpg"]]];

I want to display image using the URL link in the code, but there is not any result.

Pratik.M
  • 3
  • 3
  • Why is this question tagged with `NSDate`? Also, do not put theses 4 executions in one line. Separate each one, and check the value of each one to understand where the issue may lie. – Larme May 14 '17 at 18:35
  • Sorry for that, i tried that solution but no image display...i separate all execution, all have some data. but i can't get result...@Larme – Pratik.M May 14 '17 at 18:53
  • url is right one...you can try. copy that URL and paste it in browser...u will get image...@Maddyヅヅ – Pratik.M May 14 '17 at 19:01
  • 1
    Maybe worth pointing out that in the browser, the url actually becomes https://safesocpgm.000webhostapp.com/images/Profile/Pratik%20Maniya%20Photo.jpg (with spaces escaped). There is a string function `addingPercentEncoding` that might be useful. http://stackoverflow.com/questions/24551816/swift-encode-url – Samantha May 14 '17 at 19:04
  • As pointed by Samantha, the URL is not valid. Just writing `NSURL *url = [NSURL URLWithString:@"https://safesocpgm.000webhostapp.com/images/Profile/Pratik Maniya Photo.jpg"];` gives nil, so I doubt that you tried separately and check the values. Escape the spaces. – Larme May 14 '17 at 20:45

1 Answers1

0

The problem is in the spaces symbols in url.

You can use:

stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]

Full code:

NSString *base_url = @"https://safesocpgm.000webhostapp.com/images/Profile/Pratik Maniya Photo.jpg";
NSString* encoded_url = [base_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
_profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:encoded_url]]];
Ivan Kramarchuk
  • 226
  • 6
  • 17