Ces*_*sar 2 iphone user-interface asynchronous image download
我已经编写了一个小类来使用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] 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];
}
Run Code Online (Sandbox Code Playgroud)
完成后设置图像并更新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];
}
}
Run Code Online (Sandbox Code Playgroud)
这个系统工作得很好,但是我无法在UITableViewCell的单元格内更新UIImageView,任何想法?(实际上当我点击它时它更新的单元格)有更好的方法来实现这个功能吗?(实际上需要的是:非阻塞,缓存系统)
小智 6
塞萨尔
对于异步映像加载,缓存和线程操作,我使用http://github.com/rs/SDWebImage中的 SDImageCache类.我也写了好几次,但是这个很棒,而且我把所有旧代码扔掉了.
从其文件:
只是
#import
在UIImageView+WebCache.h
头,并调用setImageWithURL:placeholderImage:
从方法tableView:cellForRowAtIndexPath:
UITableViewDataSource方法.从异步下载到缓存管理,一切都将为您处理.
希尔顿
归档时间: |
|
查看次数: |
3828 次 |
最近记录: |