簡體   English   中英

自動布局,將圖片異步加載到uiimageview中(Swift,xCode6)

[英]Autolayout, async load image into uiimageview (Swift, xCode6)

美好的一天! 我有2個問題,希望您能幫助我。

1)我的應用程序中有新聞提要,其中包含圖像。 我正在為動態單元格使用自動版式: http://i.stack.imgur.com/DGWua.png

我希望圖像保持其比例並完全填充單元格的寬度( margins = 12 )。 我設置了約束,單元格可以自動調整大小 ,但是圖像未保存其比例:

http://i.stack.imgur.com/OSMfz.png

我做錯了什么?

2)第二個問題,我異步加載圖像,這是我的代碼:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: EventCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as EventCell
    var rowData: NSDictionary = tableData[indexPath.row] as NSDictionary
    cell.titleButton.setTitle(rowData["title"] as? String, forState: UIControlState.Normal)
    cell.titleButton.addTarget(self, action: "openWebSite:", forControlEvents: UIControlEvents.TouchUpInside)
    cell.titleButton.tag = indexPath.row
    cell.descriprionLabel.text = rowData["description"] as? String
    var urlString = rowData["image"] as String
    var image = imageCache[urlString]
    if( image == nil ) {
        var imgURL = NSURL(string: urlString)

        // Download an NSData representation of the image at the URL
        var request: NSURLRequest = NSURLRequest(URL: imgURL!)
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
            if error == nil {
                image = UIImage(data: data) // data -> image
                // Store the image in to our cache
                self.imageCache[urlString] = image // save in our dictionary
                if let cellToUpdate : EventCell = tableView.cellForRowAtIndexPath(indexPath) as? EventCell {
                    self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
                }
            }
            else {
                println("Error: \(error.localizedDescription)")
            }
        })
    } else {
        dispatch_async(dispatch_get_main_queue(), {
            if let cellToUpdate : EventCell = tableView.cellForRowAtIndexPath(indexPath) as? EventCell  {
                cellToUpdate.img.image = image
            }
        })
    }
    cell.selectionStyle = UITableViewCellSelectionStyle.None;
    cell.contentView.setNeedsLayout(); // autolayout bug solution
    cell.contentView.layoutIfNeeded(); // autolayout bug solution
    return cell
}

一切似乎都還可以,但是UITableViewCell不會在加載圖像時調整大小,而我正在嘗試在索引路徑處重新加載單元格。
有趣的時刻,如果我向下滾動然后回到單元格,它將起作用。
我之前也遇到過類似的錯誤,並且已閱讀這篇文章的UITableView布局(在推送搜索和返回時弄亂了),將其修復 (iOS 8,Xcode beta 5,Swift) ,第三個答案。 但這現在對我沒有幫助。 看來我需要調用一些方法來重新計算UITableViewCell ,但是我不明白。

第一個問題:將UIImageView視圖模式從“縮放”更改為“填充”到“寬高比”(在Storyboad中)

第二個問題:如果image不是nil,則取消調度異步,並使您的代碼看起來像這樣:

if( image == nil ) {
...
}
else {
    cell.img.image = image       
}

對於情節提要中的第一個,請選擇圖像視圖,然后單擊圖釘圖標以設置自動布局約束並檢查寬度和高度。 它將保持寬度和高度不變。

暫無
暫無

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

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