【问题标题】:How to use completion block using SDWebImage in Swift 3.0?如何在 Swift 3.0 中使用 SDWebImage 完成块?
【发布时间】:2016-11-16 05:23:37
【问题描述】:

我正在使用SDWebImage 下载图像。如果图片下载成功,我想做进一步的操作。

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. 
})

但我收到错误:

无法将类型 '(UIImage!, NSError!, SDImageCacheType, URL!) -> Void' 的值转换为预期的参数类型 'SDExternalCompletionBlock?'

【问题讨论】:

  • 我收到此错误...无法将类型 '(UIImage!, NSError!, SDImageCacheType, URL!) -> Void' 的值转换为预期的参数类型 'SDExternalCompletionBlock?'
  • 这不应该是评论,应该与您的问题一起发布
  • 我的代码是:cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage.init(named: "App-Default"), completed: {(image: UIImage!, error: NSError! , cacheType: SDImageCacheType, imageURL: URL!) -> Void in // 执行操作。})
  • 我正在使用更新的 SDWebImage 库。

标签: ios swift3 sdwebimage


【解决方案1】:

终于解决了。

cell.appIcon.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
 // Perform operation.
}) 

【讨论】:

  • 谢谢伙计!我的错误是传递 SDWebImageOptions: nil。这解决了它。 :)
【解决方案2】:

SWIFT 4-5 版本

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
})

重要! 不要忘记在必要时将 self 作为弱或无主(如 [self weak]/[self unowned])发送到块中,以避免保留循环。

例子:

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
        }
)

【讨论】:

    【解决方案3】:

    更新: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
    }) 
    

    试试这个,希望能正常工作

    【讨论】:

      【解决方案4】:

      根据您正在使用的框架中的typedef

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

      SDExternalCompletionBlock_Nullable 指示的可选参数组成。因此你的代码应该这样写:

      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. 
      })
      

      由于编译器知道完成块参数的类型(来自函数声明),您可以更简洁地编写代码并且(IMO)更易于阅读,如下所示:

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

      【讨论】:

      • 我已经实现了第一个..出现错误..无法使用类型为 '(with: URL, placeholderImage: UIImage?, completed: (UIImage?, NSError?, SDImageCacheType, URL?) -> Void)'
      • 如果实施第二个..得到错误..'sd_setImage'的模糊使用
      • 自动补全给出 cell.appIcon.sd_setImage(with: URL?, placeholderImage: UIImage?, completed:SDExternalCompletionBlock?)
      • 尝试了上次更新的代码..出现错误..“sd_setImage”的使用不明确
      • 终于完成了我使用...cell.appIcon.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), 完成: { (image, error, cacheType, imageURL) in // 执行操作。})
      【解决方案5】:

      这也适用于 swift 3:

      cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"), options: []) { (image, error, imageCacheType, imageUrl) in
                  // Perform your operations here.
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多