簡體   English   中英

UICollectionView滾動性能不佳

[英]Bad Performance on UICollectionView Scrolling

我有一個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
    }

如果有人可以幫助,那就太好了,謝謝

使用Haneke

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM