I have try to download image and set to image view, but cell layout is not calculated correct, but when we are try to scroll tableView then cell layout is correct.
func setUpCellDataWithImage(indexPath : IndexPath) {
let imageNum = String(describing: indexPath.row)
countryName.text = imageNum
let url : NSString = (imageUrl + imageNum) as NSString
if let image = getImageFromUrl(urlString: url) {
self.imageIcon.image = image
return
} else {
//Download New Image
self.downloadImageData(url: url)
}
}
func getImageFromUrl(urlString : NSString) -> UIImage?{
if let image = imageCache.object(forKey: urlString){
return image
}
return nil
}
func downloadImageData(url : NSString) {
DispatchQueue.global(qos: .background).async {
ImageDataModel().downloadImageFromUrl(urlString: url) { image in
//Downloaded Image
DispatchQueue.main.async {
self.imageIcon.image = image
self.imageCache.setObject(image!, forKey: url)
self.setNeedsLayout()
self.layoutIfNeeded()
}
}
}
}
Comments
Post a Comment