IPhone: problem loading images from NSURL

I have an array of image urls. I want to show images in a UIImageView.

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

But it takes a lot of time.

Is there a better way when I can upload images faster and better?

+3
source share
5 answers

Despite all the answers saying that you do this in one line of code, this, unfortunately, has nothing to do with the speed of the URL connection or the decoding of data / images. If you need a faster way to type the code, then that’s fine, but I would use the 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

...

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

( !) . , !:)

+2

.

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];
+1
[UIImage imageWithData:[NSData dataWithContentsOfURL:]]
0
source
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
0
source

try the following: -

image is a UIImage and image is a 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];
0
source

All Articles