gpt4 book ai didi

ios - UIImageView 滚动暂停一秒钟

转载 作者:行者123 更新时间:2023-12-01 17:41:56 25 4
gpt4 key购买 nike

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

这是我使用的代码

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];

我也尝试过后台队列。但是图像加载真的很慢。
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];
});
});
}

我不确定我做错了什么。也许我没有掌握一个概念。

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

提前感谢您的帮助。

最佳答案

您之前代码中的问题是您正在创建 UIImage来自主队列上的 GIF 数据。由于解码 GIF(尤其是具有许多帧的 GIF)可能需要一些时间,因此您阻塞了主线程,这就是为什么每次需要显示另一张图像时表格 View 都会暂时停止滚动的原因。

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

您需要做的是创建 UIImage在后台队列上,然后分派(dispatch)回主队列以更新 UIImageViewimage属性(property)。因此:

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

关于ios - UIImageView 滚动暂停一秒钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16313585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com