2012-05-22 210 views
0

我正在異步下載表中的圖像。當表格第一次出現時,connecion:didrecieveData不會被調用,但當我向上或向下移動時,它會被調用並顯示圖像。好心幫異步圖像加載

表視圖類:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    customiseForVideos *cell =(customiseForVideos *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     [[NSBundle mainBundle] loadNibNamed:@"customiseForVideos" owner:self options:nil]; 
     cell=tblCell; 
    } 
    else{ 
     AsyncImageView* oldImage = (AsyncImageView*) 
     [cell.contentView viewWithTag:999]; 
     [oldImage removeFromSuperview]; 

    } 
    [cell.imageActivity startAnimating]; 

    CGRect frame; 
    frame.size.width=65; frame.size.height=70; 
    frame.origin.x=24; frame.origin.y=12; 
    AsyncImageView* asyncImage = [[[AsyncImageView alloc] 
            initWithFrame:frame] autorelease]; 
    asyncImage.tag = 999; 
    NSURL* url = [NSURL URLWithString:[[videoCollection objectAtIndex:indexPath.row] videoImageUrl]]; 
     //NSLog(@"%@",url); 
    [asyncImage loadImageFromURL:url activity:cell.imageActivity]; 

    [cell.contentView addSubview:asyncImage]; 

    if((indexPath.row%2)==0) 
     cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"green-cell-row.png"]] autorelease]; 
    else{ 
     cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue-cell-row.png"]] autorelease]; 
    } 
    return cell; 
} 

AsyncImageView類:

- (void)loadImageFromURL:(NSURL*)url activity:(UIActivityIndicatorView *)cellActivity { 
    activityOnCell=cellActivity; 
    activityOnCell.hidden=NO; 
    if (self.connection!=nil) { [self.connection release]; } //in case we are downloading a 2nd image 
    if (data!=nil) { [data release]; } 

    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //notice how delegate set to self object 
    NSLog(@"%@",request); 
     //TODO error handling, what if connection is nil? 
} 


//the URL connection calls this repeatedly as data arrives 
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData { 
    if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; } 
    [data appendData:incrementalData]; 
} 

//the URL connection calls this once all the data has downloaded 
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection { 
    //so self data now has the complete image 
    [self.connection release]; 
    self.connection=nil; 
    if ([[self subviews] count]>0) { 
     //then this must be another image, the old one is still in subviews 
     [[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also) 
    } 

    //make an image view for the image 
    UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease]; 
    //make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed. 
    imageView.contentMode = UIViewContentModeScaleAspectFit; 
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight); 
    [self addSubview:imageView]; 
    imageView.frame = self.bounds; 
    [imageView setNeedsLayout]; 
    [self setNeedsLayout]; 
    activityOnCell.hidden=YES; 

    [data release]; //don't need this any more, its in the UIImageView now 
    data=nil; 
} 

回答

2

將圖像加載到TableView中是一個常見問題。以至於解決方案有所下降。你最好使用其中的一種,然後試圖設計你自己的。您將節省大量時間,通常會找到一個優化的解決方案,並且如果您需要更多功能,您總是可以進行子類化。

每當新圖像進來時重新加載表格並不是最佳解決方案。當你重新加載時,如果用戶正在滾動,他會失去他的位置。

使用https://github.com/AFNetworking/AFNetworking

這裏是多麼容易做你所需要的。

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; 

[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]]; 
+0

我需要導入AFnetworking包後導入我的課程? –

1

你接收到的圖像後,你是不是重裝的tableview。使用

[mtableView reloadData]; 

in connection didfinished loading。如果它不是tableview所在的類,那麼再創建另一個方法,並在其中放置重載表,並通過委託從其他類調用它。

0

有一個很大的第三方軟件包將在一行代碼爲你做的一切: https://github.com/rs/SDWebImage

所有你需要做的是導入該類別(#import "UIImageView+WebCache.h"

,然後在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}你應該用一個簡單的方法設置單元格的形象:

[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"noPic.png"]]; 

和你做:)

+0

親切指導我如何在我的項目 –

+0

用這個包我進口SDWebImage pachage後面臨這樣的錯誤:「‘_OBJC_CLASS _ $ _ SDWebImageManager’,從引用: 」 –

+0

你可以只複製所有的.h和.m文件到你的項目,然後只需將UIImageView + WebCache.h導入到你正在使用的任何類中,或者你可以繼續並按照他們的說明如何導入整個項目,它出現在GitHub鏈接中。 –

0

使用NSCache臨時存儲圖像,並使用url作爲鍵和NSData作爲值。

和方法cellforRowAtIndexPath從NSCache對象獲取值並將其分配給imageview。它將停止閃爍並且可見。