13

Am using SDWebImage to download image. I want to do further operation if image is downloaded successfully.

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage.init(named: "App-Default"), completed: {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: URL!) -> Void in
      // Perform operation. 
})

But I am getting error:

Cannot convert value of type '(UIImage!, NSError!, SDImageCacheType, URL!) -> Void' to expected argument type 'SDExternalCompletionBlock?'

Vinodh
  • 5,262
  • 4
  • 38
  • 68
Parvezkhan
  • 381
  • 1
  • 2
  • 15
  • I am getting this error...Cannot convert value of type '(UIImage!, NSError!, SDImageCacheType, URL!) -> Void' to expected argument type 'SDExternalCompletionBlock?' – Parvezkhan Nov 16 '16 at 05:25
  • 2
    That should not be a comment, that should be posted with your question – Rajat Nov 16 '16 at 05:26
  • my code is : cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage.init(named: "App-Default"), completed: {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: URL!) -> Void in // Perform operation. }) – Parvezkhan Nov 16 '16 at 06:03
  • I am using updated SDWebImage library. – Parvezkhan Nov 16 '16 at 06:07

5 Answers5

17

Finally solved.

cell.appIcon.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
 // Perform operation.
}) 
Pang
  • 9,564
  • 146
  • 81
  • 122
Parvezkhan
  • 381
  • 1
  • 2
  • 15
13

SWIFT 4-5 version

let url = URL(string: "http://some_url")
cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { image, error, cacheType, imageURL in
     // your rest code
})

Important! Don't forget to send self as weak or unowned (like this [self weak]/[self unowned]) to the block, when it is necessary, to avoid retain cycles.

Example:

cell.appIcon.sd_setImage(
     with: url,
     placeholderImage: UIImage(named: "App-Default"),
     options: SDWebImageOptions(rawValue: 0),
     completed: { [self weak] image, error, cacheType, imageURL in
                  guard let selfNotNil = self else { return }
                  // your rest code
        }
)
korgx9
  • 1,234
  • 22
  • 30
11

Updates: SWIFT 5 SDWebImage 5.x.x

        cell.imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
        cell.imageView.sd_setImage(with: url) { (image, error, cache, urls) in
            if (error != nil) {
                // Failed to load image
                cell.imageView.image = UIImage(named: "ico_placeholder")
            } else {
                // Successful in loading image
                cell.imageView.image = image
            }
        }

========================================================================

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (img, err, cacheType, imgURL) in
     // code
}) 

Try this, hope this will work fine

Ankit Kumar Gupta
  • 3,994
  • 4
  • 31
  • 54
Muhammad Raza
  • 952
  • 7
  • 24
3

According to the typedef in the framework you're using:

typedef void(^SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL);

an SDExternalCompletionBlock consists of optional parameters as indicated by _Nullable. Therefor your code should be written like this:

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage.init(named: "App-Default"), completed: {(image: UIImage?, error: NSError?, cacheType: SDImageCacheType, imageURL: URL?) -> Void in
      // Perform operation. 
})

Since the compiler knows the types of the completion block's parameters (from the function declaration) you can write the code more succinctly and (IMO) easier to read like this:

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"), completed: { (image, error, cacheType, imageURL) in
      // Perform operation. 
})
jjatie
  • 5,152
  • 5
  • 34
  • 56
  • I have implemented first one..Getting error..Cannot invoke 'sd_setImage' with an argument list of type '(with: URL, placeholderImage: UIImage?, completed: (UIImage?, NSError?, SDImageCacheType, URL?) -> Void)' – Parvezkhan Nov 16 '16 at 06:57
  • If implement second one..Getting error..Ambiguous use of 'sd_setImage' – Parvezkhan Nov 16 '16 at 07:00
  • Autocomplete gives cell.appIcon.sd_setImage(with: URL?, placeholderImage: UIImage?, completed:SDExternalCompletionBlock?) – Parvezkhan Nov 16 '16 at 07:07
  • Tried last updated code..Getting error..Ambiguous use of 'sd_setImage' – Parvezkhan Nov 16 '16 at 07:15
  • 1
    Finally done I used...cell.appIcon.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in // Perform operation. }) – Parvezkhan Nov 16 '16 at 07:40
  • Thank you jjatie for your time and support. – Parvezkhan Nov 16 '16 at 07:41
  • ah, I didn't see the API including `options:`. You don't need to (and shouldn't) force unwrap `url` – jjatie Nov 16 '16 at 15:10
2

This one also works with swift 3 :

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"), options: []) { (image, error, imageCacheType, imageUrl) in
            // Perform your operations here.
}
Ankit Kumar Gupta
  • 3,994
  • 4
  • 31
  • 54