我已經寫了一個類來執行使用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];
}
和更新的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];
}
}
這個系統工作得很好,但是我不能在UITableViewCell的單元內更新UIImageView,任何想法? (實際上是它點擊它時更新的單元格) 有更好的方法來實現這個功能嗎? (實際上需要有:無阻塞,緩存系統)
它工作的很好,但我仍然對UITableCellView – Cesar 2010-10-28 04:27:33
有同樣的問題,而且緩存系統也不能滿足我太多... – Cesar 2010-10-29 01:06:34
對我來說很完美。 – Praveenkumar 2012-12-12 09:35:22