I have the following code at cellForRowAt
function:
let url = URL(string: myurl!)
cell.empPhoto.image? = (cell.empPhoto.image?.circle)!
cell.empPhoto.image? = (cell.empPhoto.image?.rounded)!
cell.empPhoto.sd_setImage(with: url)
I'm using sd_setImage
from SDWebImage to download the image from url and cache it , but sometime the url is not found or has empty content, so how to give a default image for the cell if the url is empty , should I check the content of the url before using sd_setImage
or I can just do it from the same library ?
what I want is something like this :
if url.hasImage {
cell.empPhoto.sd_setImage(with: url)
}
else{ // use default image
cell.empPhoto.image=UIImage(named: "contact")
}
You can use this version of sd_setImage
method:
cell.empPhoto.sd_setImageWithURL(url, placeholderImage:UIImage(imageNamed:"placeholder.png"))
placeholderImage will be the default image if the url does not contains a valid image.
Additional Note: you might want to check Kingfisher, it is built inspired by SDWebImage, but is it built using Swift.