我目前正在开发一个应用程序,该应用程序包含图像下载和在滚动视图中显示大约 20 个图像。下载效果很好,从 NSData 到 UIImage 的转换也是如此。但是,如果多个下载同时完成,我会遇到 1 秒的延迟峰值,因此如果 UIImageView 的多个图像属性设置得非常接近。我怎样才能减少这种滞后?
这是下载完成时调用的代码。这可以在 1 秒的间隔内调用 20 次左右。
// _imageDataQueue is a dispatch_queue created in the init method
dispatch_async(_imageDataQueue, ^{
// data is an NSData object set when the download completes
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
if (image) {
// this is stripped down, but it is just some simple logic
// and eventually the image is set
[theImageView setImage:image];
} else {
// called if there is no image and theImageView's image is set
// to a cached image
[theImageView setImage:someCachedUIImage];
}
});
});
也许我可以将 setImage 调用排队?解决此问题的最佳方法是什么?