views:

35

answers:

1

I've got the following code that loads on an UIImageView the image that I want from the internet:

NSString* mapURL = @"http://mydomain.com/image-320x480.png";
NSData* imageData = [[NSData alloc]initWithContentsOfURL:url];

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

I'd like instead of hardcoding the URL of the image, to load a string of another URL and then load that image on the UIImageView.

My URL returns just another URL in text format.

For example: http://mydomain.com/url returns http://mydomain.com/image-320x480.png

How could I accomplish this task?

+3  A: 

Use:

NSData *imageData = 
   [[NSData alloc] initWithContentsOfUrl:
       [NSString 
           stringWithContentsOfUrl: 
             [NSURL URLWithString:@"http://mydomain.com"]
           encoding: NSUTF8StringEncoding
           error: nil
       ]
   ];

Then proceed as you were already doing.

UIImage* image = [[UIImage alloc] initWithData:imageData];
[marcaBackground setImage:image];
[imageData release];
[image release];
Pablo Santa Cruz
Excellent reply. Just needs correction of typo at initWithContentsOfUrl for initWithContentsOfURL. Thanks!
Cy.