How do I reduce lag when loading image with URLSession?

27 Views Asked by At

There is a lag in loading the UIImageView called profilePic. How can I get rid of this lag?

The problem seems to be in my addImage() method.

    
    private func addImage() {
        let donorId = donor.lastName
        guard let urlString = self.defaults.value(forKey: "url\(donorId)") as? String,
              let url = URL(string: urlString) else {
            return
        }
        let task = URLSession.shared.dataTask(with: url, completionHandler: { data, _, error in
            guard let data = data, error == nil else {
                self.donorsViewController.handle(error)
                return
            }
 
            DispatchQueue.main.async {
                let image = UIImage(data: data)
                self.profilePic.image = image
            }
        })
        task.resume()
    }
    
0

There are 0 best solutions below