簡體   English   中英

UITableView上的Choppy滾動顯示來自URL的圖像

[英]Choppy scroll on UITableView displaying images from URL

我有一個問題,我的表視圖在滾動時是不穩定的,當它加載cellForItemAtIndexPath函數內的每個圖像我已經搜索了一些例子,這些是我嘗試的東西,仍然有相同的問題。

所以我有這個

var arrRes = [[String:AnyObject]]()

然后內部視圖加載我做一個Alamofire GET請求和swiftyJSON我將json文件存儲到上面的字典。

if let resData = swiftyJsonVar["events"].arrayObject {
  self.arrRes = resData as! [[String:AnyObject]]
}
self.tableview2.reloadData()




func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1

}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return arrRes.count
}



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("mapCell", forIndexPath: indexPath) as! locationEventsTableViewCell

    var dict = arrRes[indexPath.row]

    cell.eventTitle.text = dict["eventName"] as? String

    let cal = dict["eventStarttime"] as? String



    let dateF = NSDateFormatter()
    dateF.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let date:NSDate = dateF.dateFromString(cal!)!


    let d = NSDateFormatter()

    d.dateFormat = "dd-MM-yyyy HH:mm"
    let d1 = d.stringFromDate(date)
    cell.eventDate.text = d1
    let at = dict["eventStats"]?["attendingCount"] as? Int
    let kapa = at?.stringValue
    cell.eventAttends.text = kapa
    let imageDef : UIImage = UIImage(named: "noimage")!


    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {
    if let theImage = dict["eventCoverPicture"] as? String {

        let url = NSURL(string: theImage)

        if url != nil {
            let data = NSData(contentsOfURL: url!)
            dispatch_async(dispatch_get_main_queue()) {
            cell.eventImage.image = UIImage(data: data!)

            }

        } else {
            cell.eventImage.image = imageDef
        }


    }
    }



    return cell

}

因此,你可以看到我使用調度異步函數來獲取圖像,即使我有它,它仍然不穩定。

有沒有人對此有任何解決方案? 謝謝!

所以問題是你每次顯示UITableView時都會從URL調用圖像。 每次單元格離開屏幕並返回時,它會調用方法從服務器檢索圖像。

在UI嘗試執行時正在執行服務器調用,這包括滾動和其他可視加載。

根據應用程序的不同,您可以在加載tableView之前下載UITableView的所有圖像並將其存儲在本地。 我也會研究NSCache因為這可能對你的應用程序更好。

目標是讓UI永遠是第一優先。 因此,如果有一些事情需要在UITableView中像eventCoverPicture ,在加載UITableView之前加載它們或從內存中調用它們。

  1. 這可確保您減少用戶網絡負載所需的最少量服務器調用。

  2. 用戶界面被中斷,您的用戶可以滾動瀏覽他們的應用,而不會出現這種不穩定因素

我認為你的代碼是關於Async api的。 它可能是NSDateFormatter減慢了你的速度。 日期格式化程序是一個繁重的API。 使用記憶,它也會提高性能。

class MyObject {

    // define static variable

    private static let formatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        return formatter
    }()

    // you could use it like so

    func someMethod(date: NSDate) -> String {

        return MyObject.formatter.stringFromDate(date)
    }
}

暫無
暫無

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

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