iOS-Swift 3-SDWebImage

标签 ios swift sdwebimage

我将我的代码从 Swift 2 更新到 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);

错误信息是

"Cannot convert value of type '(UIImage!, NSError!, SDImageCacheType, Bool, NSURL!) -> Void' to expected argument type 'SDWebImageCompletionWithFinishedBlock!'"

谁能帮我解决这个错误?谢谢。

最佳答案

完成 block 的签名是这样的:

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

您需要进行以下更改:

  1. NSError 更改为 Error
  2. NSURL 更改为 URL
  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")
}

关于iOS-Swift 3-SDWebImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39763279/

相关文章:

iphone - 如何在没有任何缓存的情况下为一个实例使用 SDWebImage

ios - 分配 : *** error for object: Invalid pointer dequeued from free list *** set a breakpoint in malloc_error_break to debug in Magical record IOS

swift - 混合 Swift 和 Objective-C

ios - 删除可选的 Alamofire(以及任何地方)

ios - 有什么方法可以阻止 UIImageView 中的图像在 iOS 上使用 SDWebImage 延迟加载?

swift - 如何使用 sdwebimage 框架 swift 将图像从 url 传递到另一个 View Controller

ios - 按下 iOS 时按钮没有动画

iOS GLKViewController 嵌套 GLKit View

ios - 如何在 swift 2.3 中按升序对数组进行排序

具有 Void 返回类型与无返回类型的 Swift 函数