I have a UICollectionView
which shows some data from CoreData entity. This entity has a lot of attributes and one of them is image (transformable type).
My UICollectionView
scrolling is not smooth. I think the reason is a big pictures in image attribute (from 300 to 500k).
Here is the code of my cellForItemAtIndexPath
:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *d = (AppDelegate *)[[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = @"cellRecipe";
RecipeCell *cell = (RecipeCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.image.layer.borderColor = [UIColor blackColor].CGColor;
cell.image.layer.borderWidth = 1.0f;
Recipes *recipe = [recipesArray_ objectAtIndex:indexPath.item];
if ([recipe.isPaid integerValue] == 0)
[cell.freeImage setHidden:NO];
else [cell.freeImage setHidden:YES];
NSObject *object = [[NSUserDefaults standardUserDefaults] objectForKey:@"com.________.fullhomechef"];
if ([object isEqual:[NSNumber numberWithBool:YES]]) {
[cell.freeImage setHidden:YES];
}
if ([recipe.isFavorite integerValue] == 0)
[cell.favImage setImage:[UIImage imageNamed:@"toFavorite.png"]];
else [cell.favImage setImage: [UIImage imageNamed:@"toFavorite_.png"]];
if ([recipe.isFitness integerValue] == 1)
[cell.fitImage setHidden:NO];
else [cell.fitImage setHidden:YES];
if ([d.currLang isEqualToString:@"ru"])
[cell.recipeName setText: recipe.nameru];
else [cell.recipeName setText: recipe.nameen];
[cell.image setImage:recipe.image];
int difficulty = [recipe.difficulty intValue];
[cell.difficultyImage setImage: [mainTabController imageForRating:difficulty]];
return cell;
}
I've tried to comment line [cell.image setImage:recipe.image];
but scrolling is still
intermittent.
What's wrong am I do? May be there is some method how to work with big images? Cuz in previous versions I have used images with size 50-70k.
P.S. If I patiently scroll it down to the end, scrolling become more smooth.
After the answer of Guto Araujo
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cellRecipe";
RecipeCell *cell = (RecipeCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.image.layer.borderColor = [UIColor blackColor].CGColor;
cell.image.layer.borderWidth = 1.0f;
Recipes *recipe = [fetchRecipes objectAtIndexPath:indexPath];
if ([recipe.isPaid integerValue] == 0) {
[cell.freeImage setHidden:NO];
}
else [cell.freeImage setHidden:YES];
if (wasPaid == YES) {
[cell.freeImage setHidden:YES];
}
if ([recipe.isFavorite integerValue] == 0)
[cell.favImage setImage:[UIImage imageNamed:@"toFavorite.png"] ];
else [cell.favImage setImage: [UIImage imageNamed:@"toFavorite_.png"]];
if ([recipe.isFitness integerValue] == 1)
[cell.fitImage setHidden:NO];
else [cell.fitImage setHidden:YES];
if ([d.currLang isEqualToString:@"ru"]) {
[cell.recipeName setText: recipe.nameru];
} else [cell.recipeName setText:recipe.nameen];
__weak typeof(self) weakSelf = self;
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
UIImage *image = recipe.image;
dispatch_async(dispatch_get_main_queue(), ^{
// Set via the main queue if the cell is still visible
if ([weakSelf.collectionView.indexPathsForVisibleItems containsObject:indexPath]) {
RecipeCell *cell =
(RecipeCell *)[weakSelf.collectionView cellForItemAtIndexPath:indexPath];
cell.image.image = image;
}
});
}];
[thumbnailQueue addOperation:operation];
int difficulty = [recipe.difficulty intValue];
[cell.difficultyImage setImage: [mainTabController imageForRating:difficulty]];
return cell;
}