【问题标题】:Load many gif to UICollectionView将许多 gif 加载到 UICollectionView
【发布时间】:2014-10-16 07:00:24
【问题描述】:

当我下载gif 图像并添加到收藏视图时遇到问题。 gif 图像下载得很好,但是当我尝试非常快速地滚动时它会崩溃

请帮忙。我有两个解决方案

    /*
    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; 

【问题讨论】:

    标签: ios xcode uicollectionview uicollectionviewcell


    【解决方案1】:

    你需要改变你的方法。

    您必须先加载所有图像,然后才能将它们设置为UICollectionViewCell

    假设创建一个包含所有gif 图像的NSArray。图片加载成功后,将其设置到单元格中。

    通过直接从您的代码中观察,我看到您在 cellForItemAtIndexPath 方法中从主包加载图像。所以很明显,它需要一些时间非常少(纳秒)。但是当单元格中有大量数据时,这也被认为是很大的。

    而且这条线是可能的

    [NSData dataWithContentsOfFile:[[NSBundle mainBundle]
    

    当您非常快速地滚动时将返回nil

    如果仍然不清楚,请添加评论。

    编辑:

    在后台加载图片不会影响UI,滚动会更流畅。

    另外,将try-catch 块放入该方法以检查您错过了什么。

        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);
        }
    

    【讨论】:

    • 我应该在 viewDidLoad 创建 NSArray 并将我所有的 gif 推入其中,然后从这个数组中获取我的 gif(我不应该从主包中获取它)
    • 检查这一行gifImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%li", (long)indexPath.row] ofType:@"gif"]]];它表明您正在从路径获取数据。
    • 是的,z 有 84 个 gif。我应该在 viewDidLoad 创建 NSArray 并将我所有的 gif 推入其中,然后从这个数组中获取我的 gif(我不应该从主包中获取它)。但是这个数组也会占用内存。当我第一次滚动时,它并不流畅。 (然后没关系)请告诉我也许我应该使用缓存?我理解你吗?
    • 您不需要缓存,因为这些图像在主包中。你有大量的图像。您无法加载到数组中。所以在后台线程中执行此操作,将解决您的滚动问题。
    • 请你解释一下我应该在哪里使用后台线程?在 viewDidLoad for (int i = 0; i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2019-10-03
    • 2012-05-04
    • 2013-02-19
    • 2013-03-13
    相关资源
    最近更新 更多