tags:

views:

454

answers:

3

Hi, I am experiencing some issues when I load a UIImage from a URL into a UIImageView on the iPhone. When I generate the NSURL (with the URL in the argument of NSURL's URLWithString method) using properties of an object, the image is not retrieved at all, however if I hardcode the same URL, the image is retrieved and displayed as expected.
The [item valueForKey:@"pictureLocation"]; part below seems to be what is causing the problem because even when two hardcoded strings are concatenated, the NSURl is generated with no issues.

NSString * imagePath = @"http://localhost:3000";
NSString * specificPath = (NSString *)[item valueForKey:@"pictureLocation"] ; 
//concatenate the strings to get a fully formed URL
NSString * finalPath = [imagePath stringByAppendingString:specificPath]; 

UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:finalPath]]] retain];

Essentially, when I NSLog the finalPath and instead hardcode that URL into the program, I get the expected result.

Any ideas as to why this might be the case??

Thanks.

A: 

How is the NSString returned by valueForKey: formed? Is it something like "image.png", or is it "/image.png"? If it's the former, you'll want to use stringByAppendingPathComponent: instead of stringByAppendingString:.

Cinder6
Thanks for your response...the URL formed from this code is correct and displays the image when input into a browser...
What's the URL string look like after the string has been concatenated?
Cinder6
it's fully formed and resolves correctly in a browser:http://localhost:3000/pictures/picture.jpg
So it it `@"localhost:3000..."`, or is it `@"http://localhost:3000..."`? Your initial question says the latter, but make sure, because NSURL won't work without the http://. Another thing you could test is to see if it's storing the port correctly--try the `port` method, which returns an `NSNumber`.
Cinder6
My comment should have had http:// on the latter string--forgot to escape the URL.
Cinder6
Thanks for the quick response.My apologies, the localhost URL should have http:// in front of it..really what I am doing is hardcoding "http://localhost:3000" and then appending a string retrieved from an XML page ("/pictures/picture.jpg") and passing the resulting string to NSURL....the concatenated string WORKS if i just pass it directly in to NSURL (=source of confusion).
Subview Controller's viewdidload method:- (void)viewDidLoad { [super viewDidLoad]; imageView = [[UIImageView alloc] init]; NSString * imagePath = @"http://localhost:3000"; NSString * specificPath = (NSString *)[item valueForKey:@"picture"] ; NSString * url = [imagePath stringByAppendingString:specificPath]; NSLog(@"url = "); NSLog(url); //prints correct URL NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:url]]; UIImage* image = [[UIImage alloc] initWithData:imageData]; [imageView setImage:image]; [imageData release]; [image release];}
Try comparing the concatenated string with the hardcoded version (isEqualToString:), just to make sure they are exactly the same. Also make sure the port is getting set properly in the NSURL object (port method).
Cinder6
weird! the two strings are not equal!how do i fix this!?
hmm turns out there were some whitespace characters that came back from the XML string. I have them removed and the strings are equal but i am getting the same result.Also: NSString * port = [[nsurl port] stringValue]; NSLog(@"urlport"); NSLog(port);is not printing anything
Strange. Is the encoding for the concatenated string something different from UTF8? Also, for easier NSLog use, you can do something like NSLog(@"%@", [url port]); It's just like printf.
Cinder6
A: 

Hi.

You need to use the UIWebView and load a html <img /> inside it. At least this is a nice workaround.

Check the code here http://blog.timeister.com/2009/07/18/iphone-show-online-image/

- (void)loadImage:(NSString*)url frame:(CGRect)frame {
NSString* textHTML = @"
<html><head>
<style type=\"text/css\">
body {
background-color: transparent;
color: white;
}
</style>
</head><body style=\"margin:0\">
<img  src=\"%@\"  width=\"%0.0f\" height=\"%0.0f\"></img>
</body></html>";

NSString* html = [NSString stringWithFormat:textHTML, url, frame.size.width, frame.size.height];

if(webView == nil) {
webView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:webView];
}

[webView loadHTMLString:html baseURL:nil];
}


Adrian

Adrian Pirvulescu
I will try this.I am confused though because when I simply use the image's full URL directly in the [NSURL URLWithString] method, the image appears...
The fact it works with a hard-coded URL tells you the problem lies in the string creation. I think you'll get the same result with this solution--you need to make sure the NSURL object created with the concatenated string isn't null.
Cinder6
A: 

I think problem is related to proper encoding.

try encoding the parameters properly by using following function :

encodedparams = [params stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

So in your case :

NSString * specificPath = (NSString *)[item valueForKey:@"pictureLocation"] ;
specificPath = [specificPath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

I think this will solve your problem.

Gaurav Verma