我需要在 UICollectionView 上显示大约 300 张图像。我添加了自定义 UICollectionViewCell 和子视图 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];
}