2017-01-09 16 views
0

我有圖像的URL,我想在放置在tableView單元格中的UIImageView中顯示該圖像。 我創建了一個自定義單元格,併爲imageView添加了一個插口。 由於我正在加載新聞,因此URL發生相應變化。 注意:我正在使用Alamofire來處理我的HTTP請求。如何從URL加載圖像並將其顯示在tableView單元格中(Swift 3)

struct News { 
    let title: String 
    let text: String 
    let link: String 
    let imgUrl: String 

    init(dictionary: [String:String]) { 
     self.title = dictionary["title"] ?? "" 
     self.text = dictionary["text"] ?? "" 
     self.link = dictionary["link"] ?? "" 
     self.imgUrl = dictionary["imgUrl"] ?? "" 
    } 
} 

和加載信息到我的自定義單元格

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? newsCellTableViewCell 
    let news = newsData[indexPath.row] 
    cell?.headline.text = news.title 
    cell?.excerpt.text = news.text 
    cell?.thumbnailImage.text = news.imgUrl 
    return cell! 
} 
+1

可能重複http://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift/27712427#27712427 –

+0

@LeoDabus提出好的帖子。但由於您已經在使用Alamofire,最簡單的方法是安裝https://github.com/Alamofire/AlamofireImage庫並使用以下語法顯示圖像。這負責異步加載和所有。 'cell?.thumbnailImage.af_setImage(withURL:)' –

回答

1

這變得相當容易,因爲你正在使用Alamofire。與@Mochi的例子不同,這不會阻止界面。

下面是一個例子:

Alamofire.request(news.imgUrl).response { response in 
    if let data = response.data { 
     let image = UIImage(data: data) 
     cell?.thumbnailImage.image = image 
    } else { 
     print("Data is nil. I don't know what to do :(") 
    } 
} 

*請注意:我不能回答我之前測試這一點。你可能需要調整一下這個代碼。

0

請使用SDWebImage。這是一個imageview類別,它可以在不凍住用戶界面的情況下在後臺下載圖像,並且還提供緩存。您也可以設置佔位符圖像。佔位符顯示,直到您的實際圖像被下載。下載後,單元格Imageview中的實際圖像會自動更新,並且不需要使用任何完成處理程序。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? newsCellTableViewCell 
    let news = newsData[indexPath.row] 
    cell?.headline.text = news.title 
    cell?.excerpt.text = news.text 
    cell?.thumbnailImage.sd_setImageWithURL(NSURL(string: news.imgUrl), placeholderImage:UIImage(imageNamed:"placeholder.png")) 
    return cell! 
} 
+0

我收到一個錯誤消息:'UIImageView'類型的值沒有成員'sd_setImageWithURL' –

+0

請將sdwebImage庫添加到您的項目中https://github.com/ rs/SDWebImage –

+0

我已經添加鏈接到我的答案 –

0

您可以使用此代碼從URL獲取圖像,並可以設置一個佔位符圖像,以及。例如: -

cell.imageView?.imageFromURL(urlString: "urlString", PlaceHolderImage: UIImage.init(named: "imagename")!) 

extension UIImageView { 

public func imageFromServerURL(urlString: String, PlaceHolderImage:UIImage) { 

     if self.image == nil{ 
       self.image = PlaceHolderImage 
     } 

     URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in 

      if error != nil { 
       print(error ?? "No Error") 
       return 
      } 
      DispatchQueue.main.async(execute: {() -> Void in 
       let image = UIImage(data: data!) 
       self.image = image 
      }) 

     }).resume() 
    }} 
相關問題