2011-05-02 44 views
1

我解析一个xml文件,其中包含拇指图像的网址。我想在一个可用视图中显示这些大拇指。在tableview中加载异步图像,他们在每一次滚动消失

使用class AsyncImageView(在this website上找到),我的代码工作。

唯一的问题是:每当用户滚动uitableview,图像消失片刻...然后重新出现(我认为我每次下载图像,但我不知道)!

这是一个丑陋的效果,我希望如果图像下载,它保持可见。

这是我的代码(注意:在array_thumb有网址前解析):在你的代码或AsyncImageView

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

static NSString *CellIdentifier; 
static NSString *NibNamed; 

UIDeviceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation; 

if (deviceOrientation != UIDeviceOrientationLandscapeLeft && 
    deviceOrientation != UIDeviceOrientationLandscapeRight) 
{ 
    CellIdentifier= @"Cell1"; 
    NibNamed = @"cell_gallery"; 
} 
else 
{ 
    CellIdentifier= @"Cell2"; 
    NibNamed = @"cell_gallery_landscape"; 
} 

[[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:NULL]; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
else { 
    AsyncImageView* oldImage = (AsyncImageView*)[cell.contentView viewWithTag:1]; 
    [oldImage removeFromSuperview]; 
} 

CGRect frame; 
frame.size.width=72; frame.size.height=72; 
frame.origin.x=10; frame.origin.y=10; 
AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frame] autorelease]; 

NSString *title = [NSString stringWithFormat:@"%@", [array_title objectAtIndex:indexPath.row]]; 
UILabel *testoLabel = (UILabel*)[cell viewWithTag:2]; 
testoLabel.text = title; 


asyncImage.tag = 1; 

NSLog(@"%@", asyncImage); 

NSURL *url = [NSURL URLWithString:[array_thumb objectAtIndex:indexPath.row]]; 
asyncImage.layer.cornerRadius = 10.0; 
asyncImage.layer.masksToBounds = YES; 
[asyncImage loadImageFromURL:url]; 

[cell.contentView addSubview:asyncImage]; 


UIColor *miocolore1 = [[UIColor alloc] initWithRed:247.0/255 green:247.0/255 blue:247.0/255 alpha:1.0]; 
UIColor *miocolore2 = [[UIColor alloc] initWithRed:233.0/255 green:233.0/255 blue:233.0/255 alpha:1.0]; 

if (indexPath.row % 2 == 0) { 
    cell.contentView.backgroundColor = miocolore1; 
} 
else { 
    cell.contentView.backgroundColor = miocolore2; 

} 


return cell; 


} 

回答

1

没有任何提示,图像缓存,因此它似乎是相同的图像被重复下载。

AsyncImageView使用以下加载图像:

[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]; 

你可能想确保缓存实际上是通过改变高速缓存策略发生在这里。

我想结帐ASIHTTPRequest它有一个很好的缓存实现。

+0

ASIHTTPRequest的最后一个版本是在撰写本文时的三年前。查看[AFNetworking](https://github.com/AFNetworking/AFNetworking)。 – 2014-01-10 22:54:34

0

由于单元格被重用,您必须自己提供数据。您可以将下载的图像保存为NSData或UIImage,并提供要下载的网址,或者您可以使用已下载的数据。 这样它不会每次滚动时下载图像。