2016-09-29 124 views
0

我将我的代码从Swift 2更新到Swift 3,并且发现了SDWebImage的错误。iOS-Swift 3-SDWebImage

SDWebImageManager.shared().downloadImage(with: URL(string: book.picURL), options: .lowPriority, progress: { (min:Int, max:Int) -> Void in 

      }) { (image:UIImage!, error:NSError!, cacheType:SDImageCacheType, finished:Bool, url:NSURL!) -> Void in 
       if image != nil && finished 
       { 

        let obj = cell.keepUrl 
        if obj != nil && url != nil && obj == url 
        { 
         cell.picURL.image = image 
        } 
       } 
      } 

SDWebImageCompletionWithFinishedBlock的定义是以下

typedef void(^SDWebImageCompletionWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL); 

该错误消息

“无法将类型的值“(的UIImage!NSError!SDImageCacheType,布尔,NSURL !) - > Void'到期望的参数类型'SDWebImageCompletionWithFinishedBlock!'“

任何人都可以帮助我如何解决这个错误?谢谢。

回答

2

为完成块的签名是这样的:

typealias PrefetchingDone = (UIImage?, Error?, SDImageCacheType, Bool, URL?) -> Void 

你需要做以下更改

  1. 变化NSErrorError
  2. 变化NSURLURL
  3. 变化!?

使用,你可以写这样的方法:

class func preloadImageWithUrlString(_ urlString: String, fetchedClosure: ImageFetchedClosure? = nil) { 
    let imageURLString = addWidthParameter(urlString, width: width) 
    guard let url = URL(string: imageURLString) else { 
     // Call closure with some error... 
     fetchedClosure(nil, MyError.someCustomErrorHere, SDImageCacheTypeNone, true, nil) 
     return 
    } 

    SDWebImageManager.shared().downloadImage(with: url, options: SDWebImageOptions(rawValue: 0), progress: nil) { 
     (maybeImage, maybeError, cacheType, finished, imageURL) in 
     if let closure = completionClosure { 
      closure(maybeImage, maybeError, cacheType, url) 
     } 
    } 
} 

你使用这样的:

UIImageView.preloadImageWithUrlString("http://some.url.com/myImage.png") { 
    (maybeImage, maybeError, cacheType, finished, imageURL) in 
    print("prefetching done") 
} 
+0

它没有工作... –

+0

什么没有奏效?它没有编译? – Sajjon

+0

你是否也从'NSError'改为'Error'?我会更新我的答案,以便明确表示 – Sajjon