仅使用 Kingfisher 库获取 UIImage
Get UIImage only with Kingfisher library
我只需要获取 UIImage 而不是使用 Kingfisher library
加载普通的 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")
})
此代码运行完美,但我想以更简洁的方式进行。
该库中是否有一种方法可以仅获取 UIImage ?异步和缓存
您可以使用 KingfisherManager
的 retrieveImage(with:options:progressBlock: completionHandler:)
方法。
也许是这样的:
KingfisherManager.shared.retrieveImage(with: url, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
print(image)
})
有了新的 Swift 3 版本,您可以使用 ImageDownloader :
ImageDownloader.default.downloadImage(with: url, options: [], progressBlock: nil) {
(image, error, url, data) in
print("Downloaded Image: \(image)")
}
ImageDownloader
是 Kingfisher 的一部分所以不要忘记 import Kingfisher
.
这是在 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)")
}
}
}
如何调用上面的函数
self.downloadImage(with: imageURL) //replace with your image url
翠鸟 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)")
}
}
查看更多:https://github.com/onevcat/Kingfisher/wiki/Kingfisher-5.0-Migration-Guide
翠鸟5中的另一种方式:
KingfisherManager.shared.retrieveImage(with: url) { result in
let image = try? result.get().image
if let image = image {
...
}
}
Swift 5:
我将完成 @Hardik Thakkar 的回答以添加更改,以便您可以 return 使用图像关闭可能会帮助某人:
func downloadImage(with urlString : String , imageCompletionHandler: @escaping (UIImage?) -> Void){
guard let url = URL.init(string: urlString) else {
return imageCompletionHandler(nil)
}
let resource = ImageResource(downloadURL: url)
KingfisherManager.shared.retrieveImage(with: resource, options: nil, progressBlock: nil) { result in
switch result {
case .success(let value):
imageCompletionHandler(value.image)
case .failure:
imageCompletionHandler(nil)
}
}
}
如何调用:
downloadImage(with :yourUrl){image in
guard let image = image else { return}
// do what you need with the returned image.
}
如果你想通过 api 调用自定义 UILabel instated of UIImageView。
import Kingfisher
func getImage(imageUrl: String,title:String?=nil){
let someTitle = UILabel()
view.addSubview(someTitle)
someTitle.text = title
someTitle.isHidden = true
someTitle.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
someTitle.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
someTitle.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
someTitle.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in
let url = URL(string: imageUrl )
yourImage.kf.setImage(
with: url,
placeholder: (someTitle as? Placeholder),
options: [
.loadDiskFileSynchronously,
.cacheOriginalImage,
.transition(.fade(0.25))
],
completionHandler: { result in
// Done
switch result {
case .success(let value):
self.yourImage.image = value.image
case .failure(let error):
someTitle.isHidden = false
}
}
)
}
}
只需调用控制器中的函数
getImage(imageUrl: you_url, title: your_name)
Swift 5
翠鸟 5
此代码 100% 有效
YourImageView.kf.setImage(with: URL(string: imagePath), placeholder: nil, options: nil, progressBlock: nil, completionHandler: { result in
switch result {
case .success(let value):
print("Image: \(value.image). Got from: \(value.cacheType)")
case .failure(let error):
print("Error: \(error)")
}
})
//OR
let resource = ImageResource(downloadURL: picUrl!)
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)")
imageProfile = value.image
case .failure(let error):
print("Error: \(error)")
}
}
我只需要获取 UIImage 而不是使用 Kingfisher library
加载普通的 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")
})
此代码运行完美,但我想以更简洁的方式进行。 该库中是否有一种方法可以仅获取 UIImage ?异步和缓存
您可以使用 KingfisherManager
的 retrieveImage(with:options:progressBlock: completionHandler:)
方法。
也许是这样的:
KingfisherManager.shared.retrieveImage(with: url, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
print(image)
})
有了新的 Swift 3 版本,您可以使用 ImageDownloader :
ImageDownloader.default.downloadImage(with: url, options: [], progressBlock: nil) {
(image, error, url, data) in
print("Downloaded Image: \(image)")
}
ImageDownloader
是 Kingfisher 的一部分所以不要忘记 import Kingfisher
.
这是在 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)")
}
}
}
如何调用上面的函数
self.downloadImage(with: imageURL) //replace with your image url
翠鸟 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)")
}
}
查看更多:https://github.com/onevcat/Kingfisher/wiki/Kingfisher-5.0-Migration-Guide
翠鸟5中的另一种方式:
KingfisherManager.shared.retrieveImage(with: url) { result in
let image = try? result.get().image
if let image = image {
...
}
}
Swift 5:
我将完成 @Hardik Thakkar 的回答以添加更改,以便您可以 return 使用图像关闭可能会帮助某人:
func downloadImage(with urlString : String , imageCompletionHandler: @escaping (UIImage?) -> Void){
guard let url = URL.init(string: urlString) else {
return imageCompletionHandler(nil)
}
let resource = ImageResource(downloadURL: url)
KingfisherManager.shared.retrieveImage(with: resource, options: nil, progressBlock: nil) { result in
switch result {
case .success(let value):
imageCompletionHandler(value.image)
case .failure:
imageCompletionHandler(nil)
}
}
}
如何调用:
downloadImage(with :yourUrl){image in
guard let image = image else { return}
// do what you need with the returned image.
}
如果你想通过 api 调用自定义 UILabel instated of UIImageView。
import Kingfisher
func getImage(imageUrl: String,title:String?=nil){
let someTitle = UILabel()
view.addSubview(someTitle)
someTitle.text = title
someTitle.isHidden = true
someTitle.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
someTitle.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
someTitle.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
someTitle.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in
let url = URL(string: imageUrl )
yourImage.kf.setImage(
with: url,
placeholder: (someTitle as? Placeholder),
options: [
.loadDiskFileSynchronously,
.cacheOriginalImage,
.transition(.fade(0.25))
],
completionHandler: { result in
// Done
switch result {
case .success(let value):
self.yourImage.image = value.image
case .failure(let error):
someTitle.isHidden = false
}
}
)
}
}
只需调用控制器中的函数
getImage(imageUrl: you_url, title: your_name)
Swift 5 翠鸟 5
此代码 100% 有效
YourImageView.kf.setImage(with: URL(string: imagePath), placeholder: nil, options: nil, progressBlock: nil, completionHandler: { result in
switch result {
case .success(let value):
print("Image: \(value.image). Got from: \(value.cacheType)")
case .failure(let error):
print("Error: \(error)")
}
})
//OR
let resource = ImageResource(downloadURL: picUrl!)
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)")
imageProfile = value.image
case .failure(let error):
print("Error: \(error)")
}
}