1

I want to show image in imageview from Url and it is having space in its name…

// NSString* imageURL = @"http://dev.squealrs.com/imaging.php/image-name.jpg?height=45&noimg=100&image=/wp-content/uploads/sqbrands/GMT-July-cover1344489425.jpg";

NSString* imageURL = @"http://dev.squealrs.com/imaging.php/image-name.jpg?height=45&noimg=100&image=/wp-content/uploads/sqbrands/Sookie Stackhouse small1343970416.jpg";

NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]];

UIImage* image = [[UIImage alloc] initWithData:imageData];
[ImageWithspace setImage:image];

but no image is shown bcoz uncommented imageURL contains space in imagename, if i use commented imageURL than it display image in imageview :( ....give me some solution

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
amrita
  • 41
  • 1
  • 7

4 Answers4

5

You need to encode the url:

NSString *encoded = [imageURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
graver
  • 15,183
  • 4
  • 46
  • 62
  • Use **NSUTF8StringEncoding**. The encoding determines which bytes the percent escapes will describe. If you use ASCII, accented characters are not guaranteed to survive or, if they do, to be encoded in any specific encoding, because those characters are not in ASCII. Moreover, if any of the characters aren't in whatever encoding Cocoa graces you with (such as anything in pretty much any Asian language), the method will still return nil -[Peter Hosey](http://stackoverflow.com/users/30461/peter-hosey) – Parag Bafna Sep 12 '12 at 11:40
0

You need to encode the URL before you use it to create the NsURL. There is a built in class that will help you encode the string.

Here is an example of encoding a string for use with NSURL: NSURL Encoding in ObjC

Community
  • 1
  • 1
Anil
  • 2,539
  • 6
  • 33
  • 42
0
 NSURL *url = [[NSURL alloc] initWithString:[myurlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

This happens because the white spaces in the url are not treated hence percent encoding is used

AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
0
 NSString* imageURL = @"http://dev.squealrs.com/imaging.php/image-name.jpg?height=45&noimg=100&image=/wp-content/uploads/sqbrands/Sookie Stackhouse small1343970416.jpg";
NSString *spaceUrl = [[NSString stringWithFormat:imageURL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
spaceUrl = [spaceUrl stringByReplacingOccurrencesOfString:@" " withString:@"%20"];


NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:spaceUrl]];

UIImage* image = [[UIImage alloc] initWithData:imageData]
    [ImageWithspace setImage:image];

Just Copy paste this code and see. IF you have problem still then feel free to ask

iSanjay
  • 173
  • 11