2

I'm trying to load an image from an image URL. All the code I have researched and tried finds a few errors, a lot having to do with new ARC compatibility.

I need the image to load into an image view.

Any help appreciated.

Thanks.

James Webster
  • 31,873
  • 11
  • 70
  • 114
devsquared
  • 21
  • 1
  • 1
  • 2

2 Answers2

22

I will just adapt Jim Dovey answer from here Getting Image from URL Objective C :

Synchronous version

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];

Asynchronous version

dispatch_async(dispatch_get_global_queue(0,0), ^{
    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]];
    if ( data == nil )
        return;
    dispatch_async(dispatch_get_main_queue(), ^{
        UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: data]];
    });
});
Community
  • 1
  • 1
streem
  • 9,044
  • 5
  • 30
  • 41
1
UIImage *image = [UIImage imageWithContentsOfURL:[NSURL URLWithString:@"http://i.imgur.com/ytxO16A.png"];
James Webster
  • 31,873
  • 11
  • 70
  • 114