无法设置完成块swift 2.0 w / SDWebImageCompletionBlock

时间:2015-12-15 05:51:05

标签: ios swift sdwebimage

当我声明SDWebImageCompletionBlock时,它会在编译“使用未声明的类型类型'SDWebImageCompletionBlock'之前抛出此错误:

let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType!, imageURL: NSURL!) -> Void in
        }

当我将imageView设置为完成块时,这可以工作:

self.imgView!.sd_setImageWithURL(url, placeholderImage: UIImage(named: "my_img_placeholder"))

2 个答案:

答案 0 :(得分:2)

我发现您的代码没有任何问题。我试过它,它适用于swift 2.1和Xcode 7.2。您确定已将SDWebImage正确导入源代码吗?

这段代码对我有用:

import SDWebImage

class MyClass {
   func myFunction() {
      let block = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: NSURL!) -> Void in
         // completion code here
      }
      imageView.sd_setImageWithURL(NSURL(string: stringUrl!)!, placeholderImage: UIImage(named: "placeholder_image"), completed: block)
   }
}

答案 1 :(得分:0)

swift 3使用此

     activityIndicator.startAnimating()

     let block: SDWebImageCompletionBlock = {(image, error, cacheType, imageURL) -> Void in
     activityIndicator.stopAnimating()

}

     imageView.sd_setImage(with: picture_url, placeholderImage: UIImage(named: "placeholder"), options: