Upload lots of gifs to UICollectionView

I have a problem loading gif images and adding to the collection. gif images load well, but when I try to scroll very fast, it crashes

Please, help. I have two solutions

  /* cellForGif.layer.borderColor = [[UIColor colorWithRed:54.0/255.f green:56.0/255.f blue:67.0/255.f alpha:1.0]CGColor]; cellForGif.layer.borderWidth = 0.7; FLAnimatedImage __block *gifImage = nil; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ gifImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%li", (long)indexPath.row] ofType:@"gif"]]]; dispatch_async(dispatch_get_main_queue(), ^{ cellForGif.gifImage.animatedImage = gifImage; cellForGif.linkOnGif = [self.linksArrayOnGifs objectAtIndex:indexPath.row]; //gifImage = nil; }); }); return cellForGif; */ cellForGif.layer.borderColor = [[UIColor colorWithRed:54.0/255.f green:56.0/255.f blue:67.0/255.f alpha:1.0]CGColor]; cellForGif.layer.borderWidth = 0.7; FLAnimatedImage *gifImage = nil; gifImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%li", (long)indexPath.row] ofType:@"gif"]]]; cellForGif.gifImage.animatedImage = gifImage; cellForGif.linkOnGif = [self.linksArrayOnGifs objectAtIndex:indexPath.row]; //gifImage = nil; return cellForGif; 
+7
ios xcode uicollectionview uicollectionviewcell
source share
1 answer

You need to change your approach.

You must load all images before setting them in a UICollectionViewCell .

Let's say create an NSArray contains all the gif images. After successfully loading the images, install them in the cell.

Observing directly from your code, I see that you are loading an image from the main package in the cellForItemAtIndexPath method. Thus, it is obvious that this will take some time (nano sec). But it is also significantly significant when there is a large amount of data in the cell.

And it is possible that the line

 [NSData dataWithContentsOfFile:[[NSBundle mainBundle] 

will return nil when you scroll very fast.

Add a comment if it is not yet clear.

EDIT:

Downloading images in the background does not affect the user interface, and scrolling will be smoother.

Also put a try-catch in this method to check what you missed.

  cellForGif.layer.borderColor = [[UIColor colorWithRed:54.0/255.f green:56.0/255.f blue:67.0/255.f alpha:1.0]CGColor]; cellForGif.layer.borderWidth = 0.7; @try { FLAnimatedImage __block *gifImage = nil; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ gifImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%li", (long)indexPath.row] ofType:@"gif"]]]; dispatch_async(dispatch_get_main_queue(), ^{ cellForGif.gifImage.animatedImage = gifImage; cellForGif.linkOnGif = [self.linksArrayOnGifs objectAtIndex:indexPath.row]; //gifImage = nil; }); }); } @catch (NSException *exception) { NSLog(@"Exception :%@",exception.debugDescription) } 
+3
source share

All Articles