I'm displaying lot of images (5k images) in UICollectionView
. These images has been download from remote server. When I scroll from top to bottom and vice versa of the UICollectionView
for 5 to 6 times, my app get crashes and I received error "Terminating app due to memory pressure". Please advice to fix this issue.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return urlLists.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
UIImageView *cellImage=[[UIImageView alloc]init];
cellImage.frame=cell.contentView.bounds;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[urlLists objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData:data];
UIImageJPEGRepresentation(image, 0.1);
data=nil;
dispatch_async(dispatch_get_main_queue(), ^{
cellImage.image=image;
});
});
NSLog(@"The Indexpath=%ld", indexPath.row);
[cell.contentView addSubview:cellImage];
return cell;
}