Cy.
Cy.

Reputation: 2145

Reading string from a URL and then loading it's image in an UIImageView

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?

Upvotes: 2

Views: 887

Answers (1)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

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];

Upvotes: 3

Related Questions