gpt4 book ai didi

iOS-Swift 3-SDWebImage

转载 作者:行者123 更新时间:2023-11-28 12:39:37 26 4
gpt4 key购买 nike

我将我的代码从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com