【问题标题】:Bad Performance on UICollectionView ScrollingUICollectionView 滚动性能不佳
【发布时间】:2015-03-15 15:38:01
【问题描述】:

我有一个 UICollectionView 用于显示我从文件系统加载的图像。然而,滚动似乎有一个非常糟糕的性能,当向上和向下滚动时,它有点摇晃,并且不像它应该的那样流畅 - 与 Apple 的照片应用程序相比。

详细说明: 用户可以使用相机选择或拍照,然后将其以全分辨率保存到文件系统中。然后将文件 url 保存在核心数据数据库中。

在执行 segue 时,我来到了具有集合视图的概览页面。在这里,我通过保存在数据库中的 NSURL 加载图像,在加载时我调整了图像的大小 - 从大约 900 kb 到 45 kb。这工作正常,所以集合视图中的图像小于 50 kb,在我看来这并不算多,对吧?

代码如下:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let width = 90
    let height = 90
    let theSize = CGSize(width: width, height: height)
    return theSize
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let myImageCell:ImageCell = myCollectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as ImageCell

    var baseString:String = documentsDirectory()
    var pathComponents = [baseString, self.pictures[indexPath.row]]
    var theImageFile = NSURL.fileURLWithPathComponents(pathComponents)

    let theNewImage = self.scaleNewImage(theImageFile)// self.scaleNewImage(readImageFileFromDirectoryNSURL(self.pictures[0]))
    myImageCell.imageCell.image = theNewImage

    return myImageCell
}

//这是调整图像大小的代码

func scaleNewImage (url:NSURL) -> UIImage {
        let src = CGImageSourceCreateWithURL(url, nil)
        let scale = UIScreen.mainScreen().scale
        let value = (90) * scale // self.view.bounds.width/2
        let parameters = [
            kCGImageSourceShouldAllowFloat as String : true,
            kCGImageSourceCreateThumbnailWithTransform as String : true,
            kCGImageSourceCreateThumbnailFromImageAlways as String : true,
            kCGImageSourceThumbnailMaxPixelSize as String : value
        ]
        let imageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, parameters)
        let image = UIImage(CGImage: imageRef, scale: scale, orientation: .Up)!

        //println(image)
        //println(image.size)

        return image
    }

如果有人可以提供帮助,那就太好了,在此先感谢

【问题讨论】:

  • 你研究过图像缓存吗?
  • 不,我没有,这有帮助吗?我对 swift/iOS 很陌生,这怎么能实现?有什么例子吗? - 不需要解决方案,只要方向,谢谢
  • 方向是谷歌或页面顶部的搜索框:-)
  • 我可以说“不谢谢”我已经找过了,但是如果你从来没有对缓存做过任何事情,那么很难找到一个开始,我认为 Stackoverflow 是为了提供从开发人员到开发人员的帮助......就像我已经做过的......没关系,请在我以后的问题中,忽略我,谢谢!

标签: ios swift uicollectionview


【解决方案1】:

使用哈内克

var theImageFile = NSURL.fileURLWithPathComponents(pathComponents)
imageCell.hnk_setImageFromURL(theImageFile!)

参考:http://blog.revivalx.com/2015/02/24/uicollectionview-tutorial-in-swift-using-alamofire-haneke-and-swiftyjson/

【讨论】:

  • 感谢 Dato 的建议,但我用 Ivo 的解决方案解决了这个问题。只需要对线程进行更多调整,现在一切都可以快速顺利地进行。但我也可以试试你的解决方案......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多