ViewController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // ViewController.swift
  3. // Kingfisher-Demo
  4. //
  5. // Created by Wei Wang on 15/4/6.
  6. //
  7. // Copyright (c) 2017 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import UIKit
  27. import Kingfisher
  28. class ViewController: UICollectionViewController {
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. // Do any additional setup after loading the view, typically from a nib.
  32. title = "Kingfisher"
  33. if #available(iOS 10.0, tvOS 10.0, *) {
  34. collectionView?.prefetchDataSource = self
  35. }
  36. }
  37. override func didReceiveMemoryWarning() {
  38. super.didReceiveMemoryWarning()
  39. // Dispose of any resources that can be recreated.
  40. }
  41. @IBAction func clearCache(sender: AnyObject) {
  42. KingfisherManager.shared.cache.clearMemoryCache()
  43. KingfisherManager.shared.cache.clearDiskCache()
  44. }
  45. @IBAction func reload(sender: AnyObject) {
  46. collectionView?.reloadData()
  47. }
  48. }
  49. extension ViewController {
  50. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  51. return 10
  52. }
  53. override func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  54. // This will cancel all unfinished downloading task when the cell disappearing.
  55. (cell as! CollectionViewCell).cellImageView.kf.cancelDownloadTask()
  56. }
  57. override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  58. let url = URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.row + 1).jpg")!
  59. _ = (cell as! CollectionViewCell).cellImageView.kf.setImage(with: url,
  60. placeholder: nil,
  61. options: [.transition(ImageTransition.fade(1))],
  62. progressBlock: { receivedSize, totalSize in
  63. print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
  64. },
  65. completionHandler: { image, error, cacheType, imageURL in
  66. print("\(indexPath.row + 1): Finished")
  67. })
  68. }
  69. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  70. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell
  71. cell.cellImageView.kf.indicatorType = .activity
  72. return cell
  73. }
  74. }
  75. extension ViewController: UICollectionViewDataSourcePrefetching {
  76. func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
  77. let urls = indexPaths.flatMap {
  78. URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\($0.row + 1).jpg")
  79. }
  80. ImagePrefetcher(urls: urls).start()
  81. }
  82. }