Ankit Chauhan
Ankit Chauhan

Reputation: 2405

iPhone: Problem loading images from NSURL

I have array of image url. I want to show the images into UIImageView.

Now I convert the URL to NSData and then convert that into UIImage and then try to load that into UIImageView.

But it takes a lot of time to do this.

Is there a better way where in I can load the images in a faster and better manner?

Upvotes: 3

Views: 4022

Answers (5)

Simon Lee
Simon Lee

Reputation: 22334

Despite all of the answers on here telling you to do this in one line of code, it will sadly make no difference to the URL connection speed OR data / image decoding. If you want a faster way to TYPE the code then fine, but I would use category added to UIImageView....

@interface UIImageView (URL)

- (void)loadFromUrl:(NSString *)aUrl;

@end

@implementation UIImageView (URL)

- (void)loadFromUrl:(NSString *)aUrl {
  NSURL *url = [NSURL urlWithString:aUrl];
  NSData *data = [NSData dataWithContentsOfURL:url]
  UIImage *image = [UIImage imageWithData:data];

  if(image != nil) {
    [self setImage:image];
  }
}

@end

Now you can include the header and do...

[myImageView loadFromUrl:@"http://myurl.com/image.jpg"];

For more categories (I will be adding this one to my list!) check here. Those are all my useful ones, you may find them useful too! :)

Upvotes: 3

Gypsa
Gypsa

Reputation: 11314

try this:-

image is UIImage and imageView is UIImageView

NSData *receivedData = [NSData dataWithContentsOfURL:@"yoururl"];
self.image=nil;
UIImage *img = [[UIImage alloc] initWithData:receivedData ];
self.image = img;
[img release];
[self.imageView setImage:self.image];

Upvotes: 0

visakh7
visakh7

Reputation: 26400

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];

Upvotes: 0

Nickolay Olshevsky
Nickolay Olshevsky

Reputation: 14160

[UIImage imageWithData:[NSData dataWithContentsOfURL:]]

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

Use everything in a single statement.

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];

Upvotes: 1

Related Questions