Pat*_*onz 28 uiimage ios swift2 kingfisher
我只需要获取UIImage,而不是使用Kingfisher库加载普通的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")
})
Run Code Online (Sandbox Code Playgroud)
这段代码完美无缺,但我希望以更干净的方式完成.这个库中有一个方法只能获得UIImage吗?异步和缓存
one*_*cat 58
您可以使用此retrieveImage(with:options:progressBlock: completionHandler:)方法KingfisherManager.
也许是这样的:
KingfisherManager.shared.retrieveImage(with: url, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
print(image)
})
Run Code Online (Sandbox Code Playgroud)
Har*_*kar 14
这是在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)")
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何调用以上功能
self.downloadImage(with: imageURL) //replace with your image url
Run Code Online (Sandbox Code Playgroud)
Paw*_*weł 11
使用新的Swift 3版本,您可以使用ImageDownloader:
ImageDownloader.default.downloadImage(with: url, options: [], progressBlock: nil) {
(image, error, url, data) in
print("Downloaded Image: \(image)")
}
Run Code Online (Sandbox Code Playgroud)
ImageDownloader是翠鸟的一部分,所以不要忘记import Kingfisher.
斯威夫特 5:
我将在@Hardik Thakkar回答上完成添加更改,以便您可以使用闭包返回图像,这可能对某人有所帮助:
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何调用:
downloadImage(with :yourUrl){image in
guard let image = image else { return}
// do what you need with the returned image.
}
Run Code Online (Sandbox Code Playgroud)
在《翠鸟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)")
}
}
Run Code Online (Sandbox Code Playgroud)
查看更多:https ://github.com/onevcat/Kingfisher/wiki/Kingfisher-5.0-Migration-Guide
Kingfisher 5 中的另一种方式:
KingfisherManager.shared.retrieveImage(with: url) { result in
let image = try? result.get().image
if let image = image {
...
}
}
Run Code Online (Sandbox Code Playgroud)
雨燕 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)")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17783 次 |
| 最近记录: |