2

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?

1
  • This sounds pretty messy. Why not make http://mydomain.com/url return a proper HTTP redirect rather than your own custom scheme? Commented Oct 1, 2010 at 17:08

1 Answer 1

3

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];
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent reply. Just needs correction of typo at initWithContentsOfUrl for initWithContentsOfURL. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.