xcode UIcollectionView 的图像和滚动加载缓慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16454160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIcollectionView has slow jerky loading of images and scrolling
提问by user157733
I have a UICollectionView with some images in it. It is very slow and jerky to load the images on the first scroll down. After that, when the images are loaded it is fine.
我有一个 UICollectionView,里面有一些图像。在第一次向下滚动时加载图像非常缓慢和生涩。之后,当图像加载时就可以了。
I guess the problem is that the images don't get loaded until the cell reaches the screen which causes a lag. I'm assuming what I need to do is load the images before they appear on the screen. I'm not sure how to do this and am obviously searching for the wrong thing as I can't find any answers. Please can someone point me in the right direction please??
我想问题是图像直到单元格到达屏幕才会加载,这会导致延迟。我假设我需要做的是在图像出现在屏幕上之前加载它们。我不知道该怎么做,显然我在寻找错误的东西,因为我找不到任何答案。请有人能指出我正确的方向吗??
Here is the code which loads the images...The images are in my app not downloaded or anything.
这是加载图像的代码......图像在我的应用程序中没有下载或任何东西。
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell myImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
return cell;
}
Thanks!
谢谢!
回答by itsji10dra
回答by Michael Baltaks
Even though you are not downloading the images, loading from permanent storage is still too slow to do during scrolling, as you've discovered. Try treating the +imageNamed: call as if it was a download, which will make the code more complex, but fix the responsiveness. The answers to other questions like Poor UICollectionView Scrolling Performance With UIImagemight help.
正如您所发现的,即使您没有下载图像,在滚动期间从永久存储加载仍然太慢。尝试将 +imageNamed: 调用视为下载,这会使代码更复杂,但修复了响应性。其他问题(如使用 UIImage 的 UICollectionView 滚动性能不佳)的答案可能会有所帮助。
回答by James
Just fetch the contents and store it in an array asynchronously. And do only the assigning part from within the delegate method. If you want to display all the cells at once, place a sample thumbnail image and replace it with the original when the array gets the real image. Else, you could reload the collection view each time the array gets an object, such that the collection view's cell count increases gradually as the images get fetched.
只需获取内容并将其异步存储在数组中。并且只在委托方法中执行分配部分。如果您想一次显示所有单元格,请放置一个示例缩略图图像,并在数组获取真实图像时将其替换为原始图像。否则,您可以在每次数组获取对象时重新加载集合视图,以便随着图像的获取,集合视图的单元格计数逐渐增加。