开发者

iPhone: Problem loading images from NSURL

开发者 https://www.devze.com 2023-03-12 05:57 出处:网络
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.

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?


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! :)


Use everything in a single statement.

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


[UIImage imageWithData:[NSData dataWithContentsOfURL:]]


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


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消