ios UICollectionview 加载单元格时滚动不连贯
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18460655/
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 Scrolling choppy when loading cells
提问by Avner Barr
I have a gallery in my app utilizing a UICollectionView
. The cells are approximately 70,70 size. I am using ALAssets
from the ALAssetLibrary
in the gallery which I have stored in a list.
我的应用程序中有一个使用UICollectionView
. 细胞大小约为 70,70。我使用ALAssets
从ALAssetLibrary
我在其中已经存储在列表中的画廊。
I am using the usual pattern for populating the cells:
我使用通常的模式来填充单元格:
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
mycell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
mycell.imageView.image = [[UIImage imageWithCGImage:[alassetList objectAtIndex:indexpath.row] thumbnail]];
return mycell;
}
My gallery is scrolling choppy. I don't understand why this is. I've tried adding a NSCache to cache the thumbnail images (thinking maybe creating the images was expensive) but this did not help for the performance.
我的画廊滚动不连贯。我不明白这是为什么。我已经尝试添加一个 NSCache 来缓存缩略图(认为创建图像可能很昂贵)但这对性能没有帮助。
I would expect the UI to be as buttery as the stock app.
我希望用户界面像股票应用程序一样黄油。
I am now suspecting it may be something in the UICollectionViewCell prepareForReuse
that may be holding up the dequeueReusableCellWithReuseIdentifier
method but using instruments I was not able to find this.
我现在怀疑它可能会UICollectionViewCell prepareForReuse
阻止该dequeueReusableCellWithReuseIdentifier
方法,但使用仪器我无法找到它。
Any other thing that may be be causing this? Is there a "faster" way to prepare the UICollectionViewCell
or to dequeue
them in a faster fashion?
任何其他可能导致这种情况的事情?有没有一种“更快”的方式来以更快的方式UICollectionViewCell
为dequeue
他们做准备?
回答by Avner Barr
回答by preynolds
I would assume the "choppyness" is coming from the UIImage allocation, not anything with the dequeueReusableCellWithReuseIdentifier
method. I'd try doing the image allocation on a background thread and see if that makes things a little more buttery.
我认为“不稳定”来自 UIImage 分配,而不是该dequeueReusableCellWithReuseIdentifier
方法的任何内容。我会尝试在后台线程上进行图像分配,看看这是否会让事情变得更有趣。
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
mycell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
// Load image on a non-ui-blocking thread
UIImage *image = [[UIImage imageWithCGImage:[alassetList objectAtIndex:indexpath.row] thumbnail]];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
// Assign image back on the main thread
mycell.imageView.image = image;
});
});
return mycell;
}
More details can be found here: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html
更多细节可以在这里找到:https: //developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html
回答by Beau Hankins
Load your images using NSURLConnection's sendAsynchronousRequest:queue:completionHandler:
使用 NSURLConnection 加载您的图像 sendAsynchronousRequest:queue:completionHandler:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[cell.imageView setImage:[UIImage imageWithData:data]];
}];
回答by A.G
Tick on clip subviews and opaque in attributes inspector and tick off paging enabled.
在属性检查器中勾选剪辑子视图和不透明,并勾选启用分页。
回答by NoWhereToBeSeen
If anyone is using PhImageManager, turning off synchronous solved the problem. (deliveryMode = .FastFormat can also give additional performance enhancement, but the tradeoff is the thumbnail will be of lower quality)
如果有人在使用 PhImageManager,关闭同步就解决了这个问题。(deliveryMode = .FastFormat 也可以提供额外的性能增强,但权衡是缩略图质量较低)
let option = PHImageRequestOptions()
option.deliveryMode = .Opportunistic
option.synchronous = false
PHImageManager().requestImageForAsset(phAsset, targetSize: CGSizeMake(2048, 2048), contentMode: .AspectFit, options: option, resultHandler: { (image, objects) in
self.imageView.image = image!
})
回答by Paulo Souto
I had issues with UICollectionView
scrolling.
我在UICollectionView
滚动时遇到了问题。
What worked (almost) like a charm for me: I populated the cells with png thumbnails 90x90. I say almost because the first complete scroll is not so smooth, but never crashed anymore...
什么对我来说(几乎)就像一个魅力:我用 90x90 的 png 缩略图填充了单元格。我说几乎是因为第一个完整的滚动不是那么流畅,但再也没有崩溃过......
In my case, the cell size is 90x90.
就我而言,单元格大小为 90x90。
I had many original png sizes before, and it was very choppy when png original size was greater than ~1000x1000 (many crashes on first scroll).
我之前有很多原始 png 大小,当 png 原始大小大于 ~1000x1000 时非常不稳定(第一次滚动时发生许多崩溃)。
So I select 90x90 (or the like) on the UICollectionView
and display the original pngs (no matter the size). Hope this helps others.
所以我选择 90x90(或类似)UICollectionView
并显示原始 png(无论大小)。希望这对其他人有帮助。