gpt4 book ai didi

iphone - uicollection View 可重用单元格图像在滚动时重新加载

转载 作者:行者123 更新时间:2023-11-28 20:14:11 25 4
gpt4 key购买 nike

你好,我正在使用 UICollectionView 在我的 iPhone 应用程序中显示图像,但是当我 ScrollView 时,加载的图像消失并再次加载图像,这是因为“dequeueReusableCellWithReuseIdentifier”这是我的代码

static NSString * const kCellReuseIdentifier = @"collectionViewCell";
[self.collectionViewPack registerNib:[UINib nibWithNibName:@"CollectionViewItem" bundle:nil] forCellWithReuseIdentifier:kCellReuseIdentifier];

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifier forIndexPath:indexPath];

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
UIImageView *icon=(UIImageView *)[cell viewWithTag:101];
// [titleLabel setText:[NSString stringWithFormat:@"%d",indexPath.row]];
[titleLabel setText:[NSString stringWithFormat:@"%@",[arrayImages objectAtIndex:indexPath.row]]];
icon.image =[UIImage imageNamed:@"loading-1.png"];


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *imagePath = [NSString stringWithFormat:@"%@", [test.arrImages objectAtIndex:indexPath.row]];
NSURL *imageUrl = [NSURL URLWithString:imagePath];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = nil;
if (imageData){

image = [[UIImage alloc] initWithData:imageData];
icon.image = image;

}

[image release];

});



return cell;


}

请帮我解决这个问题。

最佳答案

我想你需要在代码中添加一些图像缓存,我建议你使用 AFNetworking ,你可以轻松地做到:

导入“UIImageView+AFNetworking.h”

……

[cell.imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"default"]];

而且,它有一个内部缓存,可以提供帮助:D

对于您的代码...尝试将其添加到您的代码块中

dispatch_async(dispatch_get_main_queue(), ^{
icon.image = image;
});

关于iphone - uicollection View 可重用单元格图像在滚动时重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18765752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com