前端之家收集整理的这篇文章主要介绍了
Swift-Kingfisher图片下载库,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import UIKit
import Kingfisher
class KingfisherViewController: UIViewController {
let url = "http://img3.cache.netease.com/game/2014/7/2/20140702132129bebcf.jpg"
@IBOutlet weak var loadedIamgeView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
/*
//MARK: - 基本用法
*/
@IBAction func request(sender: UIButton) {
requestImage4()
}
/*
//MARK: - 基本用法
*/
func request0() -> Void{
self.loadedIamgeView.kf_setImageWithURL(NSURL(string: url)!)
//带有默认图片的用法
self.loadedIamgeView.kf_setImageWithURL(NSURL(string: url)!,placeholderImage: nil)
}
/*
//MARK: - Kingfisher是带有缓存的,在请求之前会在(内存或者硬盘)找此图片,如果没有才会发起网络请求
如果你想每次都是从网络加载的图片,可以在一个参数字典中设置一下,让请求忽略点缓存
*/
func requestImage1() -> Void{
self.loadedIamgeView.kf_setImageWithURL(NSURL(string: url)!,placeholderImage: nil,optionsInfo:[.Options(KingfisherOptions.ForceRefresh)])
}
/*
//MARK: - 请求下图片来后,会有一定时间的渐变 慢慢显示在ImageView上面
*/
func requestImage2() -> Void{
self.loadedIamgeView.kf_setImageWithURL(NSURL(string: url)!,optionsInfo: [.Transition(ImageTransition.Fade(1))])
}
/*
//MARK: - 擦 怎么进度打印不出来啊 只能得到image、缓存类型、和image的地址
*/
func requestImage3() -> Void{
self.loadedIamgeView.kf_setImageWithURL(NSURL(string: url)!,optionsInfo: nil,progressBlock: { (receivedSize,totalSize) -> () in
dispatch_async(dispatch_get_main_queue(),{ () -> Void in
print("receive\(totalSize)")
})
}) { (image,error,cacheType,imageURL) -> () in
print(image?.size.width)
}
}
func requestImage4() -> Void{
//kf_setImageWithURL 每个方法都会返回一个RetrieveImageTask的对象,可以调用cancel方法取消下载图片
let task = self.loadedIamgeView.kf_setImageWithURL(NSURL(string: url)!)
task.cancel()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application,you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
原文链接:https://www.f2er.com/swift/325129.html