2010-10-28 86 views
2

我已經寫了一個類來執行使用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,任何想法? (實際上是它點擊它時更新的單元格) 有更好的方法來實現這個功能嗎? (實際上需要有:無阻塞,緩存系統)

回答

6

塞薩爾

對於異步圖像加載,緩存和螺紋操作中,我使用SDImageCache類從http://github.com/rs/SDWebImage。我也寫了幾次,但這個很棒,我把所有舊代碼都扔了。

從它的文檔:

只是#importUIImageView+WebCache.h頭,並調用從tableView:cellForRowAtIndexPath: UITableViewDataSource方法setImageWithURL:placeholderImage:方法。從異步下載到緩存管理,一切都將爲您處理。

希爾頓

+0

它工作的很好,但我仍然對UITableCellView – Cesar 2010-10-28 04:27:33

+1

有同樣的問題,而且緩存系統也不能滿足我太多... – Cesar 2010-10-29 01:06:34

+0

對我來說很完美。 – Praveenkumar 2012-12-12 09:35:22

1
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init]; 
    [img_data setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.str_url]]]]; 
    [pool release]; 
1

該系統的工作原理相當不錯,但我無法得到更新的UIImageView 一個UITableViewCell的細胞內,任何想法? (實際上單元格 它更新時,我點擊它)有一個更好的方法來實現 這個功能? (實際上需要有:無阻塞,緩存 系統

您是否嘗試過使用[的tableView reloadData]; 在connectionDidFinishLoading方法結束說它

3

只需按照以下步驟:

1) Download SDWebImage below And drag to your xcode project. 

Click here to Download the SDWebImage

2) Write the following code in your .h file #import "UIImageView+WebCache.h" 
    3) Write the following code for your image view 
    [yourImageView setImageWithURL:[NSURL URLWithString:@"www.sample.com/image.png"]]; 

那就是它......你已經完成了!

+0

嘗試使用SDWebImage,但在運行時遇到8個錯誤。 「compat」 - 版本提出了10.是否有任何提示? – alexdd55 2013-03-31 10:26:00

+0

你使用ARC嗎? – VSN 2013-04-01 04:52:38

+0

是的,我願意。項目需要將MapKit.framework和ImageIO.framework添加到構建階段中的「Link Binary With Libraries」中。 – alexdd55 2013-04-02 21:52:33

1

簡單地使用SDWebImage。我也在使用那個。加載圖像流暢。我使用下面的代碼來顯示URL

[myImageView setImageWithURL:[NSURL URLWithString:@"www.sample.com/image.png"] placeholderImage:[UIImage imageNamed:@"lod.png"] 
success:^(UIImage *image, BOOL cached) 
{ 
    productImageDetails.image = image; 
} 
failure:^(NSError *error) { 
    productImageDetails.image =[UIImage imageNamed:@"no_image.jpg"]; 
}]; 

成功部分圖像將WebService的顯示圖像。而且,如果有任何圖像無法加載或其他故障問題,您可以簡單地加載自己的圖像。該例子中的There是更多的功能。你可以參考它。