I tried to set a tableview
background image, it works fine when i load image from assets. But i want to load image from web url it doesn't work.
var imageView = UIImageView(frame:CGRect(x: 0, y: 0, width: 100, height: 200))
//let image = UIImage(named: "default_no_logo")
let image = self.fetchImage(url: "https://example.com/assets/image/store_logos/f10689d075cdfd672868c9cb4c2b9034.jpeg")
cell.backgroundColor = UIColor.clear
imageView = UIImageView(image:image)
cell.backgroundView = imageView
I have this function to download image from url, but it always return "default_no_logo", please help me.
func fetchImage(url: String) -> UIImage {
let imageURL = URL(string: url)
var image: UIImage?
if let url = imageURL {
DispatchQueue.global(qos: .userInitiated).async {
let imageData = NSData(contentsOf: url)
DispatchQueue.main.async {
if imageData != nil {
image = UIImage(data: imageData! as Data)
}
}
}
}
return image ?? UIImage(named: "default_no_logo")!
}