我在我的项目中设置了一个 uicollectionview,它从 JSON 文件获取数据。一切正常,但是,滚动速度非常慢,当 View 滚动即将到来的单元格时,片刻会显示之前单元格的内容。
我尝试过使用 dispatch_async
,但它仍然非常缓慢且不稳定。
知道我做错了什么吗?
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("VideoCell", forIndexPath: indexPath) as UICollectionViewCell
let communityViewController = storyboard?.instantiateViewControllerWithIdentifier("community_id")
videoCell.frame.size.width = (communityViewController?.view.frame.size.width)!
videoCell.center.x = (communityViewController?.view.center.x)!
videoCell.layer.borderColor = UIColor.lightGrayColor().CGColor
videoCell.layer.borderWidth = 2
let fileURL = NSURL(string:self.UserVideosInfo[indexPath.row][2])
let asset = AVAsset(URL: fileURL!)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
//self.showIndicator()
let NameLabelString = self.UserVideosInfo[indexPath.row][0]
let CommentLabelString = self.UserVideosInfo[indexPath.row][1]
let DateLabelString = self.UserVideosInfo[indexPath.row][3]
let buttonPlayUserVideo = videoCell.viewWithTag(1) as! UIButton
let nameLabel = videoCell.viewWithTag(2) as! UILabel
let commentUserVideo = videoCell.viewWithTag(3) as! UILabel
let dateUserVideo = videoCell.viewWithTag(4) as! UILabel
let thumbUserVideo = videoCell.viewWithTag(5) as! UIImageView
let deleteUserVideo = videoCell.viewWithTag(6) as! UIButton
buttonPlayUserVideo.layer.setValue(indexPath.row, forKey: "indexPlayBtn")
deleteUserVideo.layer.setValue(indexPath.row, forKey: "indexDeleteBtn")
dispatch_async(dispatch_get_main_queue()) {
nameLabel.text = NameLabelString
commentUserVideo.text = CommentLabelString
dateUserVideo.text = DateLabelString
self.shadowText(nameLabel)
self.shadowText(commentUserVideo)
self.shadowText(dateUserVideo)
if let cgImage = try? assetImgGenerate.copyCGImageAtTime(time, actualTime: nil) {
thumbUserVideo.image = UIImage(CGImage: cgImage)
}
}
}
//THIS IS VERY IMPORTANT
videoCell.layer.shouldRasterize = true
videoCell.layer.rasterizationScale = UIScreen.mainScreen().scale
return videoCell
}
最佳答案
起初 - 您正在使用来自全局队列的 UI 对象,看起来没有任何目的。这是被禁止的 - 否则行为将是不确定的。
次要,最繁重的操作是创建您在主队列上执行的缩略图。
考虑使用 AVAssetImageGenerator
的方法
public func generateCGImagesAsynchronouslyForTimes(requestedTimes: [NSValue], completionHandler handler: AVAssetImageGeneratorCompletionHandler)
而不是你自己的异步。
第三,viewWithTag
是相当繁重的操作,导致对 subview
进行枚举。考虑在单元格中为您需要的 View 声明属性。
UPD:要在单元格中声明属性,请创建具有适当属性的 UICollectionViewCell
的子类作为 IBOutlets。然后,在您的 View Controller viewDidLoad
实现中,调用
collecionView.registerClass(<YourCellSubclass>.dynamicType, forCellWithReuseIdentifier:"VideoCell")
或者,如果您的 Collection View 单元格是在 Storyboard 中配置的,请在 Interface Builder 的单元格设置窗口中指定单元格的类并将其 subview 直接连接到类的导出。
第四步,您的单元格被 Collection View 重复使用。每次您的单元格离开可见区域时,它都会从 Collection View 中删除并放入重用队列。当您滚动回单元格时,您的 View Controller 将再次被要求提供一个单元格。并且您正在为每个新出现的单元格再次获取视频的缩略图。考虑通过 collectionView 的 indexPath.item
索引将它们存储在某个数组中来缓存已获取的缩略图。
关于ios - SWIFT 2 - UICollectionView - 慢速滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447406/