4

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")
     }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Zizoo
  • 1,694
  • 5
  • 20
  • 42
  • while setting image check length of string or check string contains expected domain name, if result false then set your expected image to image view. – Suresh.Chandgude Oct 05 '16 at 16:26
  • @Io.s-c. Thanks , but it's not like that , all urls have the same domain but with deferent image name every employee image stored with his name , and some of them doesn't have images so here comes my problem – Zizoo Oct 05 '16 at 16:30
  • Is your image have any extension like png, jpeg etc.. then check if that is present in url string. simple. – Suresh.Chandgude Oct 05 '16 at 16:32
  • @io.s-c. All urls are images url with jpeg extension , but some of these urls are not found – Zizoo Oct 05 '16 at 16:34
  • Ohh, So you are saying that url is correct but no image present there. in that case you can check sd image object contains image. By using this " if image != nil " you can check it. – Suresh.Chandgude Oct 05 '16 at 16:34
  • have you tried this? – Suresh.Chandgude Oct 05 '16 at 16:56

4 Answers4

12

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.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • Thanks but I get error from calling this method `sd_setImageWithURL` – Zizoo Oct 05 '16 at 17:00
  • That depends on the version of Swift that you are using. Probably you are using Swift 3, it should be like: cell.empPhoto.sd_setImage(with: url, placeholderImage: UIImage(imageNamed:"placeholder.png")) – Ahmad F Oct 05 '16 at 17:03
2

It is better to have placeholder image so there will still be an image even when your request is failed.

You can set the placeholder using SDWebImage function sd_setImageWithURL:placeholderImage

Swift

cell.empPhoto.sd_setImage(with: url, placeholderImage: placeholderImage);

Objective-C

[cell.empPhoto.image sd_setImageWithURL:url placeholderImage:placeholderImage];
ramacode
  • 914
  • 6
  • 14
1

try this code too:

Swift 3 & SDWebImage 4.0.0

//SDWebImage 4.0.0
self.profileImageView.sd_setImage(with: yourImageUrl, placeholderImage: UIImage("Your placeholder image name")
-1
if let imageUrl = dicData?.image, let url = URL(string: imageUrl ) {
    cell.imgMain.sd_setImage(with: url, placeholderImage: UIImage(named: "YOUR_IMAGE_NAME"), options: SDWebImageOptions.continueInBackground)
} else {
    cell.imgMain.image = UIImage(named: "YOUR_IMAGE_NAME")
}
HangarRash
  • 7,314
  • 5
  • 5
  • 32