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.
    }
    */

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...