2012-02-15 11 views
0

私は少し問題があります。私は、AsyncImageViewクラスを使用してxmlファイルのテーブルビューに画像を表示しようとします。このクラスはWebサイトで見つけられ、問題は私はゆっくりと画像をスクロールダウンし、これはコードasyncimageview低速レンダリング - uitableview

#import "AsyncImageView.h" 


@implementation AsyncImageView 

- (void)dealloc { 
    [connection cancel]; //in case the URL is still downloading 
} 


- (void)loadImageFromURL:(NSURL*)url { 
    //in case we are downloading a 2nd image 

    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //notice how delegate set to self object 
} 


//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 
    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]]; 
    imageView.contentMode = UIViewContentModeScaleAspectFit; 

    [self addSubview:imageView]; 
    imageView.frame = self.bounds; 
    [imageView setNeedsLayout]; 

    [self setNeedsLayout]; 

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

- (UIImage*) image { 
    UIImageView* iv = [[self subviews] objectAtIndex:0]; 
    return [iv image]; 
} 

@end 

と、これはテーブルコード

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 
    else { 
     AsyncImageView* oldImage = (AsyncImageView*) 
     [cell.contentView viewWithTag:999]; 
     [oldImage removeFromSuperview]; 
    }  
    elemento = [array objectAtIndex:indexPath.row]; 

    NSString *indirizzoImmagine = [elemento objectForKey:@"Descrizione"]; 

    CGRect frame; 
    frame.size.width=88; 
    frame.size.height=88; 
    frame.origin.x=0; 
    frame.origin.y=0; 

    AsyncImageView* asyncImage = [[AsyncImageView alloc] initWithFrame:frame]; 

    asyncImage.tag = 999; 
    NSURL *url = [NSURL URLWithString:indirizzoImmagine];  
    [asyncImage loadImageFromURL:url]; 

    cell.accessoryView = asyncImage;  

    return cell; 
} 

です...私はスクロールダウンした場合にすぐに画像をロードするためにいくつかの秒を取る、ほとんどすぐにロードされていますどのようにテーブルビューを最適化できますか?

答えて

0

あなたのネットワークは、それぞれの画像を読み込むのに必要な速度に追いついていない可能性があります。最初に、あなたの画像がフレームサイズに近い小さなものであることを確認します。また、画像を読み込んでいて、iOSで表のセルをリサイクルする方法では、リサイクルされたセルの画像を読み込んでいる可能性があります。この場合、oldImage参照の負荷をキャンセルするだけで問題はありません。

関連する問題