在桌面视图中加载异步图像,它们在每次滚动时消失
我正在解析一个 xml 文件,其中包含缩略图的 url。我想在 uitableview 中显示这些拇指。
使用 AsyncImageView 类(可在 此网站< /a>),我的代码有效。
唯一的问题是:每次用户滚动 uitableview 时,图像都会消失一会儿......然后重新出现(我认为我每次都在下载图像,但我不确定)!
这是一个丑陋的效果,我希望如果图像被下载,它仍然可见。
这是我的代码(注意:在 array_thumb 中有之前解析的网址):
- (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;
}
I'm parsing an xml file, containing urls for thumb images. I want to show those thumbs in an uitableview.
Using class AsyncImageView (found on this website), my code works.
The only problem is this: every time that the user scrolls the uitableview, the images disappear for a moment... then re-appear (i think that i'm downloading the image every time but i'm not sure)!
That's an ugly effect, i want that if the image is downloaded, it stay visible.
this is my code (NB: in array_thumb there are the urls parsed before):
- (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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码或 AsyncImageView 中没有任何内容表明图像被缓存,因此看起来相同的图像被重复下载。
AsyncImageView 使用以下内容加载图像:
您可能希望通过更改缓存策略来确保缓存实际上发生在此处。
我会查看 ASIHTTPRequest 它有一个很好的缓存实现。
Nothing in your code or in AsyncImageView suggest that the images are cached so it seems the same image gets downloaded repeatedly.
AsyncImageView is using the following to load the image:
You probably want to make sure that caching is actually happening here by altering the cache policy.
I'd checkout ASIHTTPRequest which has a great cache implementation.
由于单元格被重复使用,您必须自己提供数据。您可以将下载的图像保存为 NSData 或 UIImage 并提供下载的 url,也可以使用已下载的数据。
这样它就不会在每次滚动时下载图像。
As the cell are reused, you have to provide the data yourself. Either you can save the downloaded image as NSData or UIImage and provide the url to download or you can use the already downloaded data.
This way it wont download the image everytime you scroll.