Im trying to upload a photo to my tableView through a url. The code I have between the "/////" comments is making my UI laggy and slow and Im not understanding why or how to fix it.
I have done some research but this doesnt seem to be working(still lagging)
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)
})
}
}
But im not sure exactly if this is the reason or how to implement it or why.
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!
}