异步图像下载器 [英] Async image downloader

查看:179
本文介绍了异步图像下载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个小类来通过使用NSURLConnection执行图像的下载。
这个想法是将下载委托给这个类,以避免阻止执行。



所以我将目标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] setNetworkActivityIndi​​catorVisible: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] setNetworkActivityIndi​​catorVisible: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 the UIImageView+WebCache.h header, and call the setImageWithURL:placeholderImage: method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.

Hilton

这篇关于异步图像下载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆