微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐