我試圖通過一個網址將照片上傳到我的tableView。我在「/////」註釋之間的代碼讓我的界面變得遲鈍而緩慢,我不明白爲什麼或者如何修復它。NSURL photo Swift
我做了一些研究,但是,這並不似乎工作(仍然滯後)
let userProfileChatImage = generalRoomDataArr[indexPath.row].photoURL
if let url = NSURL(string: userProfileChatImage!) {
if let data = NSData(contentsOf: url as URL) {
DispatchQueue.main.async(execute: {() -> Void in
imageView.image = UIImage(data: data as Data)
})
}
}
但是我不知道到底這是否是原因還是如何實現它,或者爲什麼。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let usernameLabel = cell?.viewWithTag(1) as! UILabel
usernameLabel.text = generalRoomDataArr[indexPath.row].username
let messageLabel = cell?.viewWithTag(2) as! UILabel
messageLabel.numberOfLines=0 // line wrap
messageLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
messageLabel.text = generalRoomDataArr[indexPath.row].message
//initialize UI Profile Image
let imageView = cell?.viewWithTag(3) as! UIImageView
//Make Porfile Image Cirlce
imageView.layer.cornerRadius = imageView.frame.size.width/2
imageView.clipsToBounds = true
/////////////////////////////////////////////
let userProfileChatImage = generalRoomDataArr[indexPath.row].photoURL
if let url = NSURL(string: userProfileChatImage!) {
if let data = NSData(contentsOf: url as URL) {
imageView.image = UIImage(data: data as Data)
/////////////////////////////////////////////
// your cell coding
return cell!
}