How to use completion block using SDWebImage in Swift 3.0?

后端 未结 5 1385
孤城傲影
孤城傲影 2021-01-04 12:35

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

cell.appIcon.sd_setImage(with: url, placeholderIma         


        
相关标签:
5条回答
  • 2021-01-04 12:58

    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. 
    })
    
    0 讨论(0)
  • 2021-01-04 13:05

    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

    0 讨论(0)
  • 2021-01-04 13:10

    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
            }
    )
    
    0 讨论(0)
  • 2021-01-04 13:14

    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.
    }
    
    0 讨论(0)
  • 2021-01-04 13:20

    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.
    }) 
    
    0 讨论(0)
提交回复
热议问题