I have a problem with a UICollectionView that I think I have narrowed down to the following area. The problem is that when I call a reload on the UICollectionView there is a flicker and 1 image is loaded followed by the correct image second. Almost like when you scroll new cells and it updates them but without any scrolling.
Now I've tested it with a plain
sampleImage.image = [UIImage imageNamed:@"LogoHome"];
And I get no flickering whatsoever so I've narrowed it down to the following code
[self.imageManager requestImageForAsset:[self.arrayPhotoAssets objectAtIndex:indexPath.row]
targetSize:CGSizeMake(imageWidth, imageHeight)
contentMode:PHImageContentModeAspectFill
options:nil resultHandler:^(UIImage *result, NSDictionary* info){
UIImageView *sampleImage = (UIImageView *)[cell viewWithTag:1];
sampleImage.image = result;
}];
which is inside of
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Is there any way that I can adjust the code to prevent the above happening? Here is the full snippet:
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
////// GALLERY COLLECTION VIEW
if(collectionView.tag == 1){
static NSString *cellIdentifier = @"galleryCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
float imageWidth = 77;
float imageHeight = 77;
UIScreen *mainScreen = [UIScreen mainScreen];
//NSLog(@"%f",mainScreen.bounds.size.width);
// IPHONE 6
if(mainScreen.bounds.size.width == 375){
imageWidth = 92;
imageHeight = 92;
}
// IPHONE 6 PLUS
if(mainScreen.bounds.size.width == 414){
imageWidth = 102;
imageHeight = 102;
}
[self.imageManager requestImageForAsset:[self.arrayPhotoAssets objectAtIndex:indexPath.row]
targetSize:CGSizeMake(imageWidth, imageHeight)
contentMode:PHImageContentModeAspectFill
options:nil resultHandler:^(UIImage *result, NSDictionary* info){
UIImageView *sampleImage = (UIImageView *)[cell viewWithTag:1];
sampleImage.image = result;
}];
return cell;
}
UICollectionViewCell *cell;
return cell;
}
I would recommend you to set sampleImage.image = nil
, before trying to call requestImageForAsset
. This will clear your image view such that will remove flickering.
UIImageView *sampleImage = (UIImageView *)[cell viewWithTag:1];
sampleImage.image = nil;
[self.imageManager requestImageForAsset:[self.arrayPhotoAssets objectAtIndex:indexPath.row]
targetSize:CGSizeMake(imageWidth, imageHeight)
contentMode:PHImageContentModeAspectFill
options:nil resultHandler:^(UIImage *result, NSDictionary* info){
sampleImage.image = result;
}];