Load many gif to UICollectionView

岁酱吖の 提交于 2019-12-04 05:19:30
Kampai

You need to change your approach.

You have to load all images before going to set them in UICollectionViewCell.

Let's say create an NSArray contains all gif images. After images are loaded successfully then set them to the cell.

By observing straight from your code, I see that you load an image from the main bundle in cellForItemAtIndexPath method. So it is obvious that it will take some time very little (nano sec). But that is also considered large when there are a large amount of data in a cell.

And it is possible that line

[NSData dataWithContentsOfFile:[[NSBundle mainBundle]

will return nil when you scroll very very quickly.

Add a comment if it is still not clear.

EDIT:

Loading images in the background will not affect UI and scrolling will be smoother.

Also, put try-catch block in that method to check what you have 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);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!