简体   繁体   English

iOS 10.3 beta - CollectionViewCell中的ImageView错误?

[英]iOS 10.3 beta - ImageView in CollectionViewCell bug?

Is anyone else having problems displaying an image (UIImageView) in a collection view cell in iOS 10.3 beta 6? 是否有其他人在iOS 10.3 beta 6的集合视图单元格中显示图像(UIImageView)时遇到问题? The cells that are initially visible show the image, but new/dequeued cells that become visible as I scroll do not display the image. 最初可见的单元格显示图像,但在滚动时变为可见的新/出列单元格不显示图像。 This is in Objective C, haven't tried it in Swift yet. 这是在Objective C中,尚未在Swift中尝试过。 Cell definition: 细胞定义:

@interface StdCollectionViewCell : UICollectionViewCell

@property (strong, nonatomic) IBOutlet UIImageView *fileImage;      // for thumbnail image of file
@property (strong, nonatomic) IBOutlet UITextView *fileTextView;    // for name or description of file
@property (nonatomic) BOOL unviewed;                                // has the user viewed this file yet

@end

From cellForItemAtIndexPath , I get a UIImage (which I have verified is not null), then set the cell's fileImage.image property: cellForItemAtIndexPath ,我得到一个UIImage (我已验证不是null),然后设置单元格的fileImage.image属性:

theCell.fileImage.image = image;                                                                                        // set the thumbnail image

I tried using setNeedsDisplay after assigning the image, but that didn't help. 我在分配图像后尝试使用setNeedsDisplay ,但这没有帮助。

The issue comes from a new feature available in iOS 10. 问题来自iOS 10中提供的新功能。

Indeed, in order to improve a poor scrolling experience in collection view, Apple decided to work around smoother scrolling. 实际上,为了改善收藏视图中糟糕的滚动体验,Apple决定解决更平滑的滚动问题。

As you can see : 如你看到的 :

Prefetching is a mechanism by which you are notified early before the collection view will need the cells to be displayed, thus providing an opportunity to prepare the cell's data source in advance of actually creating the cell. 预取是一种机制,通过该机制,您可以在集合视图需要显示单元格之前通知您,从而提供在实际创建单元格之前准备单元格数据源的机会。

Have a look at this articles : 看看这篇文章:

WWDC 2016 WWDC 2016

UICollectionView Prefetching in iOS 10 iOS 10中的UICollectionView预取

While attempting to create a simple app that exhibited the problem, I was able to figure out why the images weren't getting displayed. 在尝试创建一个展示问题的简单应用程序时,我能够找出图像未显示的原因。

Before prefetching was around (probably when iOS 9 was current, perhaps even before that), I had created code to dispatch creating/retrieving the images to another queue. 在预取之前(可能是iOS 9是当前的,甚至可能在此之前),我创建了代码来分配创建/检索图像到另一个队列。 Once they were ready, the code to assign them to the cell's UIImageView object was dispatched back to the main queue. 准备好后,将它们分配给单元的UIImageView对象的代码被分派回主队列。

In order to not waste cycles if the user scrolled the cell back off the screen before the image was ready, I checked several times to see if the cell was visible using indexPathsForVisibleItems before proceeding with the next step. 为了不浪费周期,如果用户在图像准备好之前将单元格从屏幕上indexPathsForVisibleItems ,我在进行下一步之前检查了几次是否使用indexPathsForVisibleItems查看单元格是否可见。 If the path wasn't in that array, I would just return nil for the image. 如果路径不在该数组中,我将为图像返回nil。 Also, when I was ready to assign the image to the UIImageView.image property, I used the collection view's cellForItemAtIndexPath . 此外,当我准备将图像分配给UIImageView.image属性时,我使用了集合视图的cellForItemAtIndexPath

Obviously, with prefetching, this shouldn't work, because indexPathsForVisibleItems shouldn't contain prefetched cells that aren't visible yet, and cellForItemAtIndexPath is supposed to return null if the cell isn't visible (so the message to set the image would be sent to null). 显然,通过预取,这应该不起作用,因为indexPathsForVisibleItems不应包含尚未看到的预取单元格,如果单元格不可见,则cellForItemAtIndexPath应返回null(因此设置图像的消息将是发送到null)。

During my initial debugging I was getting an image; 在我最初的调试过程中,我得到了一张图片; however I didn't notice that the cell variable I was trying to assign that image to was null! 但是我没有注意到我试图将该图像分配给的单元格变量为null!

Strangely, this code has been working in iOS 10 up until now, which would seem to indicate the prefetched cells were being included in the visible items array and cellForItemAtIndexPath was working for prefetched but not visible cells. 奇怪的是,这个代码到目前为止一直在iOS 10中工作,这似乎表明预取的单元格被包含在可见项目数组中,而cellForItemAtIndexPath正在用于预取但不可见的单元格。 It seems that has been fixed now, which caused my code to break. 现在似乎已经修复,导致我的代码破坏。

EDIT: Not so strange after all. 编辑:毕竟不是那么奇怪。 I just realized that the previous release of the app was completed using Xcode 7, which means it was built using the iOS 9 SDK, which didn't have prefetching. 我刚刚意识到应用程序的早期版本是使用Xcode 7完成的,这意味着它是使用iOS 9 SDK构建的,它没有预取。 So even though it was running on iOS 10, it didn't use the prefetching. 因此即使它在iOS 10上运行,它也没有使用预取。 When I opened the app in Xcode 8, it enabled Prefetching on the collection view by default. 当我在Xcode 8中打开应用程序时,默认情况下它在集合视图上启用了预取。 This isn't the first time I tested the app in Xcode 8, but I must not have tried scrolling that collection view before while working with Xcode 8. END EDIT. 这不是我第一次在Xcode 8中测试应用程序,但在使用Xcode 8之前我不能尝试滚动该集合视图.END EDIT。

The quick fix is to turn off the collection view's prefetching, which is what I did because this is an Enterprise app for our own internal use, not something that has to go in the App Store, so it doesn't matter if it is a little slower. 快速解决方法是关闭集合视图的预取,这就是我所做的,因为这是一个供我们自己内部使用的企业应用程序,而不是必须在App Store中运行的东西,所以它是无关紧要的慢一点。

I should have followed @matt's advice (which I have even given to others in the past :) ) and tried to produce a simple complete example first, and I probably would have figured this out before posting my question. 我应该遵循@ matt的建议(我过去曾经给过其他人:),并尝试先制作一个简单的完整例子,我可能会在发布我的问题之前想出这个。 But I had some other things coming up that day and was feeling impatient, so I posted anyway. 但那天我还有其他一些东西出现并感到不耐烦,所以无论如何我都发布了。 I'll try to learn from my mistake :). 我会尝试从我的错误中学习:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM