滚动时UICollectionView滞后

时间:2016-04-21 16:17:32

标签: swift uiscrollview uicollectionview xcode7 uicollectionviewcell

我有一个UICollectionView,其中我显示每个单元格包含png图像的单元格。当我在彼此旁边显示两个以上的单元格时,滚动将滞后,但是当我仅在彼此下方显示单元格时,滚动运行更加平滑。

在我的cellForItemAtIndexPath中,我得到了另外两个stackoverflow线程中推荐的2行coe:

    cell.layer.shouldRasterize = true;
    cell.layer.rasterizationScale = UIScreen.mainScreen().scale

它确实有所帮助,但并不多。

我获取图像的方法是对ImageView进行扩展:

import Foundation
import Alamofire

let imageCache = NSCache()

extension UIImageView {
    public func imageFromUrl(urlString: String) {
        if let image = imageCache.objectForKey(urlString) as? UIImage {
            self.image = nil
            self.image = image
        }
        else {
            self.image = nil
            Alamofire.request(.GET, urlString).responseJSON{
                response in
                if let tilbudimage = UIImage(data: response.data!){
                    self.image = tilbudimage
                    imageCache.setObject(tilbudimage, forKey: urlString)
                }
            }
        }
    }
}

在我的cellForItemAtIndexPath中,我初始化单元格图像,如:

let cell  = collectionView.dequeueReusableCellWithReuseIdentifier("postcol", forIndexPath: indexPath) as! PostColViewCell
cell.PostImg.imageFromUrl(firstImage["imgurl"].string!)

滚动时以这种方式显示单元格:

this lag

滚动时,以这种方式显示单元格不会滞后 enter image description here

这是我的整个cellForItemAtIndexPath:

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell  = collectionView.dequeueReusableCellWithReuseIdentifier("postcol", forIndexPath: indexPath) as! PostColViewCell

        cell.layer.shouldRasterize = true;
        cell.layer.rasterizationScale = UIScreen.mainScreen().scale

        cell.layer.cornerRadius = 10

        let post = self.postArray[indexPath.row]

        let firstImage = post["images"].array![0]

        if(post["newpost"].int! <= 1)
        {
            cell.NewLabel.hidden = false
        }
        else
        {
            cell.NewLabel.hidden = true
        }

        cell.ProfilePicture.imageFromUrl(post["user"]["photourl"].string!)
        cell.ProfilePicture.contentMode = .ScaleAspectFill
        cell.ProfilePicture.clipsToBounds = true

        cell.NameLabel.text = post["user"]["firstname"].string! + " " + post["user"]["lastname"].string!

        cell.PriceLabel.text = String(post["price"].int!) + " kr."

        cell.NameLabel?.userInteractionEnabled = true
        cell.NameLabel?.tag = indexPath.row
        let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "seeUser:")
        tapped.numberOfTapsRequired = 1
        cell.NameLabel?.addGestureRecognizer(tapped)

        cell.TitleText.textColor = UIColor.blackColor()
        cell.TitleText.labelSpacing = 5; // distance between start and end labels
        cell.TitleText.pauseInterval = 2.0; // seconds of pause before scrolling starts again
        cell.TitleText.scrollSpeed = 30; // pixels per second
        cell.TitleText.textAlignment = NSTextAlignment.Left; //centers text when no auto-scrolling is applied
        cell.TitleText.fadeLength = 12;// length of the left and right edge fade, 0 to disable
        cell.TitleText.font = UIFont(name: (cell.TitleText?.font.fontName)!, size: 12)!
        cell.TitleText.text = post["title"].string!

        if(cell.PostImg.image != nil)
        {
            cell.PostImg.imageFromUrl(firstImage["imgurl"].string!)
        }

        cell.PostImg.contentMode = .ScaleAspectFill
        cell.PostImg.clipsToBounds = true

        //Comment label layout
        cell.CommentLabel.text = String(post["comments"].array!.count)
        cell.CommentLabel.textColor = UIColor.whiteColor()
        // Here is the magic
        cell.CommentLabel.icon = UIImage(named: "Comment")// Set icon image
        cell.CommentLabel.icon = cell.CommentLabel.icon!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
        cell.CommentLabel.tintColor = UIColor.whiteColor()
        cell.CommentLabel.iconPadding = 5               // Set padding between icon and label
        cell.CommentLabel.iconPosition = SMIconLabelPosition.Left // Icon position
        cell.CommentLabel.textAlignment = .Right


        //Distance label layout
        if (post["distance"].double! < 1)
        {
            cell.DistanceLabel.text = LoginViewController.CurrentUser.CurrentLocation
        }
        else
        {
            let formatter = NSNumberFormatter()
            formatter.minimumFractionDigits = 0
            formatter.maximumFractionDigits = 0

            cell.DistanceLabel.text = formatter.stringFromNumber(post["distance"].double!)! + " km"
        }

        cell.DistanceLabel.textColor = UIColor.whiteColor()
        // Here is the magic
        cell.DistanceLabel.icon = UIImage(named: "Location")// Set icon image
        cell.DistanceLabel.icon = cell.DistanceLabel.icon!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
        cell.DistanceLabel.tintColor = UIColor.whiteColor()
        cell.DistanceLabel.iconPadding = 0            // Set padding between icon and label
        cell.DistanceLabel.iconPosition = SMIconLabelPosition.Left // Icon position
        cell.DistanceLabel.textAlignment = .Left

        return cell
    }

0 个答案:

没有答案