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

标签 ios sdk uiimageview uicollectionview

我需要在 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/

相关文章:

ios - PFRelation 在 Cell 上使用 UiButton 创建

iphone - 如何在 iPhone 上使用新的 Facebook GRAPH API 将照片和消息发布到 Facebook

ios - 居中 UILabel subview

ios - 从 UIImage 中移除 alpha channel

ios - 通过手势交换单元格的位置

ios - 给定电影的 URL,如何检索它的信息?

iphone - Facebook SDK : How to i get the photo properties after posting an image?

ios - 使用未合并到我的应用程序中的数据文件

build - nwjs有办法检查是否使用sdk版本吗?

android - 如何在 android 中获取 Facebook 个人资料图片?