gpt4 book ai didi

ios - UICollectionView 应用程序在 iPhone 上显示大量图像时崩溃

转载 作者:行者123 更新时间:2023-11-29 04:03:55 25 4
gpt4 key购买 nike

我需要在 UICollectionView 上显示大约 300 张图像。我添加了自定义 UICollectionViewCell 和 subview UIImageView,现在当我尝试从 Web 服务器在 UICollectionView 上显示图像时,它会显示大约 150 个图像,然后应用程序崩溃,显示内存不足。下面是我的代码,请让我知道我做错了什么。

-(void)updateView {

for (int i = 0; i < m_nPhotoCount; i ++)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSURL *imageURL = [NSURL URLWithString:[aryList objectAtIndex:i]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
if(image == nil)
{
NSLog(@"kashif");
}
else {
[self.imageArray addObject:image];
}
dispatch_sync(dispatch_get_main_queue(), ^{
[collectionView reloadData];
});
});
}
}

#pragma mark - UICollectionView Datasource

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {

return [self.imageArray count];
}


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

static NSString *CellIdentifier = @"GalleryCell";
GalleryCell *cell = (GalleryCell*)[cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.imageView.image = [self.imageArray objectAtIndex:indexPath.row];
return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

return [[UICollectionReusableView alloc] init];

}

最佳答案

您采取了错误的方法。你应该做的是,你应该一次只下载那些数量的图像(相当于 CollectionView 的可见单元格的数量)。

当滚动 Collection View 时,下载更多图像并清除或存储缓存中以前的图像。

您可以使用这个精彩的图像下载和缓存库:SDWebImage

关于ios - UICollectionView 应用程序在 iPhone 上显示大量图像时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15492292/

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