加载许多gif到UICollectionView [英] Load many gif to UICollectionView
问题描述
当我下载 gif
图片并将其添加到收藏夹视图中时,我遇到了问题. gif
图片下载得很好,但是当我尝试非常快速地滚动时,它会崩溃
I am facing problem when I download gif
images and add in to collection view.
gif
images are download well, but when i try to scroll very very quickly it's crash
请帮助.我有两种解决方法
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;
推荐答案
您需要更改方法.
您必须先加载所有图像,然后才能在 UICollectionViewCell
中进行设置.
You have to load all images before going to set them in UICollectionViewCell
.
比方说创建一个包含所有 gif
图片的 NSArray
.图像成功加载后,将其设置到单元格中.
Let's say create an NSArray
contains all gif
images. After images are loaded successfully then set them to the cell.
通过直接观察代码,我发现您是通过 cellForItemAtIndexPath
方法从主捆绑包加载图像的.因此很显然,这将花费很少的时间(纳秒).但是,当一个单元中有大量数据时,这也被认为是很大的.
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.
还有可能
[NSData dataWithContentsOfFile:[[NSBundle mainBundle]
当您非常快速地滚动时,
将返回 nil
.
will return nil
when you scroll very very quickly.
如果尚不清楚,请添加评论.
Add a comment if it is still not clear.
在后台加载图像不会影响UI,并且滚动会更平滑.
Loading images in the background will not affect UI and scrolling will be smoother.
此外,在该方法中放入 try-catch
块以检查您错过了什么.
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);
}
这篇关于加载许多gif到UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!