ios 仅使用 Kingfisher 库获取 UIImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35204620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:25:06  来源:igfitidea点击:

Get UIImage only with Kingfisher library

iosswift2uiimagekingfisher

提问by Patonz

I need to get a UIImage only instead of loading a normal UIImageView with Kingfisher library

我只需要获取 UIImage 而不是使用Kingfisher 库加载普通的 UIImageView

To realize it I implemented a workaround with UIImageView:

为了实现它,我使用 UIImageView 实现了一个解决方法:

let imageView = UIImageView()

imageView.kf_setImageWithURL(NSURL(string: cpa.imageName)!, placeholderImage: nil,
        optionsInfo: [.Transition(ImageTransition.Fade(1))],
        progressBlock: { receivedSize, totalSize in
            print("\(receivedSize)/\(totalSize)")
        },
        completionHandler: { image, error, cacheType, imageURL in
            anView!.image = image //anView IS NOT an UIImageView
            anView!.frame.size = CGSize(width: 15.0, height: 15.0)
            print("Finished")
    })

This code works perfectly, but I would like to do it in a cleaner way. Is there a method in this library to get an UIImage only? Async and cached

这段代码工作得很好,但我想以更简洁的方式来完成。这个库中是否有一种方法可以只获取 UIImage?异步和缓存

回答by onevcat

You could use the retrieveImage(with:options:progressBlock: completionHandler:)method of KingfisherManagerfor this.

为此,您可以使用 的retrieveImage(with:options:progressBlock: completionHandler:)方法KingfisherManager

Maybe something like this:

也许是这样的:

KingfisherManager.shared.retrieveImage(with: url, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
    print(image)
})

回答by Hardik Thakkar

This is latest syntax to download image in kingFisher 5 (Tested in swift 4.2)

这是在 kingFisher 5 中下载图像的最新语法(在 swift 4.2 中测试)

func downloadImage(`with` urlString : String){
    guard let url = URL.init(string: urlString) else {
        return
    }
    let resource = ImageResource(downloadURL: url)

    KingfisherManager.shared.retrieveImage(with: resource, options: nil, progressBlock: nil) { result in
        switch result {
        case .success(let value):
            print("Image: \(value.image). Got from: \(value.cacheType)")
        case .failure(let error):
            print("Error: \(error)")
        }
    }
}

How to call above function

如何调用上面的函数

self.downloadImage(with: imageURL) //replace with your image url

回答by Pawe?

With the new Swift 3 version you can use ImageDownloader :

使用新的 Swift 3 版本,您可以使用 ImageDownloader :

ImageDownloader.default.downloadImage(with: url, options: [], progressBlock: nil) {
    (image, error, url, data) in
    print("Downloaded Image: \(image)")
}

ImageDownloaderis a part of Kingfisher so don't forget to import Kingfisher.

ImageDownloader是 Kingfisher 的一部分,所以不要忘记import Kingfisher

回答by nsc9012

Another way in Kingfisher 5:

Kingfisher 5 中的另一种方式:

KingfisherManager.shared.retrieveImage(with: url) { result in
    let image = try? result.get().image
    if let image = image {
        ...
    }
}

回答by Quyen Anh Nguyen

In Kingfisher 5

在翠鸟5

imageView.kf.setImage(with: url) { result in
   switch result {
   case .success(let value):
       print("Image: \(value.image). Got from: \(value.cacheType)")
   case .failure(let error):
       print("Error: \(error)")
   }
 } 

see more: https://github.com/onevcat/Kingfisher/wiki/Kingfisher-5.0-Migration-Guide

查看更多:https: //github.com/onevcat/Kingfisher/wiki/Kingfisher-5.0-Migration-Guide

回答by Shakeel Ahmed

Swift 5

斯威夫特 5

let url = URL(string: USER_IMAGE_PATH + "\(arrGuserpic[index])")

cell.imgv.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
if (image != nil){
    cell.imgv.image = image
} 
else {
    cell.imgv.image = UIImage.init(named: "user")
}
})