异步图像下载器 [英] Async image downloader
问题描述
这个想法是将下载委托给这个类,以避免阻止执行。
所以我将目标UIImageView(由ref)和url传递给函数并开始下载:
- (void)getImage:(UIImageView **)image formUrl:(NSString *)urlStr
{
NSLog(@Downloader.getImage.url.debug:%@,urlStr);
NSURL * url = [NSURL URLWithString:urlStr];
NSURLRequest * req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:5.0];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURLConnection * c = [[NSURLConnection alloc] initWithRequest:req delegate:self];
[conn addObject:c];
int i = [conn indexOfObject:c];
[imgs insertObject:* image atIndex:i];
[c release];
}
完成设置图像并更新imageView:
- (void)connectionDidFinishLoading :( NSURLConnection *)连接
{
int i = [conn indexOfObject:connection];
NSData * rawData = [buffer objectAtIndex:i];
UIImage * img = [UIImage imageWithData:rawData];
UIImageView * imgView = [imgs objectAtIndex:i];
imgView.image = img;
[imgView setNeedsDisplay];
[conn removeObjectAtIndex:i];
[imgs removeObjectAtIndex:i];
[buffer removeObjectAtIndex:i];
if([conn count] == 0){
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}
这个系统工作得很好,但我不能在UITableViewCell的单元格中更新UIImageView,任何想法? (实际上,当我点击它更新的单元格)
有更好的方法来实现这个功能? (实际上需要的是:非阻塞,缓存系统)
Cesar
对于异步图像加载,缓存和线程操作,我使用来自 http://github.com/的SDImageCache类RS / SDWebImage 。我也写了我自己的几次,但这一个是辉煌的,我抛出了我所有的旧代码。
从它的文档:
只需
#import
UIImageView + WebCache.h
标题,并从tableView:cellForRowAtIndexPath:
UITableViewDataSource方法中调用setImageWithURL:placeholderImage:
方法。一切都将为您处理,从异步下载到缓存管理。
希尔顿
I have write a little class to perform the download of the images by using an NSURLConnection. The idea it's to delegate the download to this class to avoid to block the execution.
So I pass the target UIImageView (by ref) and the url to the function and start the download:
-(void)getImage:(UIImageView**)image formUrl:(NSString*)urlStr
{
NSLog(@"Downloader.getImage.url.debug: %@",urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:5.0];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:req delegate:self];
[conn addObject:c];
int i = [conn indexOfObject:c];
[imgs insertObject:*image atIndex:i];
[c release];
}
When it's finished set the image and update the imageView:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
int i = [conn indexOfObject:connection];
NSData *rawData = [buffer objectAtIndex:i];
UIImage *img = [UIImage imageWithData:rawData];
UIImageView *imgView = [imgs objectAtIndex:i];
imgView.image = img;
[imgView setNeedsDisplay];
[conn removeObjectAtIndex:i];
[imgs removeObjectAtIndex:i];
[buffer removeObjectAtIndex:i];
if ([conn count]==0) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}
This system works quite good, but i can't get updated the UIImageView inside the cells of a UITableViewCell, any idea ? (actually the cell it's updated when I click on it) There is a better way to implement this functionality ? (actually the need are: non-blocking, caching system)
Cesar
For async image loading, caching and threaded operation, I use SDImageCache class from http://github.com/rs/SDWebImage. I also wrote my own several times, but this one is brilliant and I've thrown all my old code away.
From its documentation:
Just
#import
theUIImageView+WebCache.h
header, and call thesetImageWithURL:placeholderImage:
method from thetableView:cellForRowAtIndexPath:
UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.
Hilton
这篇关于异步图像下载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!