Slow loading the images from URL in UITAbleView.

旧城冷巷雨未停 提交于 2019-11-28 06:45:44

问题


I'm loading the images from URL in UITableView. But it's very slow when loading an view. Here's an example,

UIImage *image = nil;
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://calcuttans.com/palki/wp-content/uploads/2009/02/kidscover-small.png"]]];

In Table view, UIButton i'm setting the background image.

Please Can you provide the sample.

FYI : I'm used the LazzyTable sample program but it's not much helpful. Can you suggest any other samples.


回答1:


Load image asynchronously

NSURL* url = [NSURL URLWithString:@"http://calcuttans.com/palki/wp-content/uploads/2009/02/kidscover-small.png"];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];


[NSURLConnection sendAsynchronousRequest:request
        queue:[NSOperationQueue mainQueue]
        completionHandler:^(NSURLResponse * response,
            NSData * data,
            NSError * error) {
    if (!error){
            NSImage* image = [[NSImage alloc] initWithData:data];
        // do whatever you want with image
    }

}];



回答2:


There are some open source libraries available for this:

  1. HJCache
  2. SDWebImage

These libraries download image in a asynchronous manner and cache it for further use.




回答3:


Try to implement AFNetworking. It uses async requests to download the image, you are currently blocking your view with every download.

You can then use an AFImageRequestOperation to download your image.




回答4:


if you load the image all download from the internet every time , it must be very slow.

I think you shuold exist your download image to the filePath , and when you will load the image , you can check whether the image has been downloaded before , if not ,then download. if it has been downloaded , you can use imageWithContentsOfFile: method to load the image




回答5:


//Make use of dispatch queue for faster processing of data. add this in viewDidLoad

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    NSData * data=[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
    [self performSelectorOnMainThread:@selector(setImage:) withObject:data waitUntilDone:YES];
});

//once data is got set the image and reload tableview

-(void)setImage:(NSData *)responseData
{
image = [UIImage imageWithData:responseData];
[tableView reloadData];
}



回答6:


maybe you can use asihttprequest to lazy load images. use ASINetworkQueues




回答7:


You've to use NSOperationQueue to make your tableview efficient.

Check this icodeblog tutorial and raywenderlich tutorial




回答8:


Have a look at this tutorial. It helped me a lot. When I was using it I was quite new to iOS in general and it was helpful not only with respect to loading images from the web.

http://www.markj.net/iphone-asynchronous-table-image/




回答9:


With AFNetworking it is more easy.

//AFNetworking

#import "UIImageView+AFNetworking.h"

[cell.iboImageView setImageWithURL:[NSURL URLWithString:server.imagen] placeholderImage:[UIImage imageNamed:@"qhacer_logo.png"]];


来源:https://stackoverflow.com/questions/14579079/slow-loading-the-images-from-url-in-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!