1

Im trying to get an image from a url link.

        NSURL *thmbnailUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:800/fishtune/movie/%@.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]];
        NSLog(@"thmbnail:%@",[NSString stringWithFormat:@"http://%@:800/fishtune/movie/%@.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]);
        NSData *data = [NSData dataWithContentsOfURL:thmbnailUrl];
        NSLog(@"movimgdata:%@",data);
        UIImage *img = [[UIImage alloc]initWithData:data];
        NSLog(@"movimg:%@",data);
        NSData *imgData = UIImagePNGRepresentation(img);

when i log thmbnail i get this "ipofserverinnetwork/fishtune/movie/Our Logo 480p back.png" Note:i didn't put the original ip.

I get null when i log data and of course img will also be null. I really dont know why I'm getting null. because when I tried entering the url in a browser it displays the image. I used the same code in another part of my app and it works but when i used it in this it fails. Is it because of the spaces between "Our_Logo_480p"?

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
jyce1492
  • 199
  • 2
  • 15
  • possible duplicate of [URLWithString: returns nil](http://stackoverflow.com/questions/1981390/urlwithstring-returns-nil) – Mike Abdullah Jan 14 '14 at 14:08

2 Answers2

0

you might need to use

NSString* escapedUrl = [yourstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
wasim
  • 698
  • 8
  • 20
0

yes it's because of spaces, use

 NSString *myUrl = [NSString stringWithFormat:@"http://%@:800/fishtune/movie/%@.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]];
   NSString *url = [myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60