UIImageView滚动暂停一秒钟

san*_*aet 1 uitableview uiimageview ios dispatch-async

我正在尝试使用uiimage-from-animated-gif将一些GIF加载到我的UITableView单元格中.但问题是,每当我尝试从我的Cache加载图像时,单元格在滚动之前会暂停一点.

这是我正在使用的代码

postGif = (UIImageView *)[cell viewWithTag:104];

if ([[ImageCache sharedImageCache] DoesExist:post[@"gif"]] == true)
{
    image = [[ImageCache sharedImageCache] GetImage:post[@"gif"]];

    postGif.image = [UIImage animatedImageWithAnimatedGIFData:image];

}else
{
    dispatch_queue_t backgroundQueue = dispatch_queue_create("cacheImage", 0);

    dispatch_async(backgroundQueue, ^{

        NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:post[@"gif"]]];
        dispatch_async(dispatch_get_main_queue(), ^{

            // Add the image to the cache
            [[ImageCache sharedImageCache] AddImage:post[@"gif"] image:imageData];

            NSIndexPath* ind = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
            [self.feedTableView beginUpdates];
            [self.feedTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:ind] withRowAnimation:UITableViewRowAnimationNone];
            [self.feedTableView endUpdates];
        });
    });
}

postGif.layer.cornerRadius = 2.0;
postGif.layer.masksToBounds = YES;

[cell.contentView addSubview:postGif]; 
Run Code Online (Sandbox Code Playgroud)

我也尝试了后台队列.但是Image的负载非常慢.

if ([[ImageCache sharedImageCache] DoesExist:post[@"gif"]] == true)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                             (unsigned long)NULL), ^(void) {
        NSData *image = [[ImageCache sharedImageCache] GetImage:post[@"gif"]];

        postGif.image = [UIImage animatedImageWithAnimatedGIFData:image];
    });

}else
{
    dispatch_queue_t backgroundQueue = dispatch_queue_create("cacheImage", 0);

    dispatch_async(backgroundQueue, ^{

        NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:post[@"gif"]]];
        dispatch_async(dispatch_get_main_queue(), ^{

            // Add the image to the cache
            [[ImageCache sharedImageCache] AddImage:post[@"gif"] image:imageData];

            NSIndexPath* ind = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
            [self.feedTableView beginUpdates];
            [self.feedTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:ind] withRowAnimation:UITableViewRowAnimationNone];
            [self.feedTableView endUpdates];
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

我不确定我做错了什么.也许我没有抓住一个概念.

Maycoff建议我不要从后台队列修改用户界面对象(如UIImageView)的属性,并且我需要在后台队列上创建UIImage的实例,有人可以向我解释一下我不是什么抓?

感谢您的帮助.

rob*_*off 6

您之前的代码中的问题是您是UIImage从主队列上的GIF数据创建的.由于解码GIF(尤其是具有许多帧的GIF)可能需要一些时间,因此您阻止了主线程,这就是每次需要显示另一个图像时表视图暂时停止滚动的原因.

您以后的代码中的问题是您正在修改后台队列上的image属性UIImageView.您只能修改主队列上的用户界面对象.

你需要做的是创建UIImage一个后台队列,然后派遣回主队列更新UIImageViewimage属性.从而:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                         (unsigned long)NULL), ^(void) {
    NSData *data = [[ImageCache sharedImageCache] GetImage:post[@"gif"]];
    UIImage *image = [UIImage animatedImageWithAnimatedGIFData:data];
    dispatch_async(dispatch_get_main_queue(), ^{
        postGif.image = image;
    }
});
Run Code Online (Sandbox Code Playgroud)