-3

How can I convert image from URL (String) to Image in XCode (Objective-C)? I have an images in URL. And I want to make the Array with this images but how images. Than I can read they in my IPhone aplicatioon. Thanks for help!

1

4 Answers 4

13
NSData *data = [NSData dataWithContentsOfURL : url];
UIImage *image = [UIImage imageWithData: data];

Just remember that -dataWithContentsOfURL: is a synchronous call and should not be performed on the main thread.

2
  • 1
    +1 for pointing out sync.
    – bryanmac
    May 10, 2013 at 11:46
  • 1
    Also note that -dataWithContentsOfURL: ignores any cookies whatsoever, so if you are attempting to access a file that is protected by login, you'll need to use the much more complex NSURLConnection class. May 10, 2013 at 12:04
5

This is how to do it,

NSString *imageUrlString = @"";
NSURL *url = [NSURL URLWithString:imageUrlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
3
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]]];
3
  • Please explain your answer in words as well. May 10, 2013 at 12:02
  • It creates img from the data obtained from the URL.
    – Girish
    May 10, 2013 at 12:06
  • 1
    The hell with words... This dude is AWESOME! I just used exactly that line and it was EXACTLY what I was looking for. Still working in iOS 8. Apr 9, 2015 at 20:19
1

I suggest you to use some kind of Image loader like this one.

It will load the image in background and set it to an ImageView when it's ready. It gives a way better experience as user.

Not the answer you're looking for? Browse other questions tagged or ask your own question.