ProcessorCollectionViewController.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // ProcessorCollectionViewController.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/11/19.
  6. //
  7. // Copyright (c) 2019 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. private let reuseIdentifier = "ProcessorCell"
  29. class ProcessorCollectionViewController: UICollectionViewController {
  30. var currentProcessor: ImageProcessor = DefaultImageProcessor.default {
  31. didSet {
  32. collectionView.reloadData()
  33. }
  34. }
  35. var processors: [(ImageProcessor, String)] = [
  36. (DefaultImageProcessor.default, "Default"),
  37. (ResizingImageProcessor(referenceSize: CGSize(width: 50, height: 50)), "Resizing"),
  38. (RoundCornerImageProcessor(radius: .point(20)), "Round Corner"),
  39. (RoundCornerImageProcessor(radius: .widthFraction(0.5), roundingCorners: [.topLeft, .bottomRight]), "Round Corner Partial"),
  40. (BorderImageProcessor(border: .init(color: .systemBlue, lineWidth: 8)), "Border"),
  41. (RoundCornerImageProcessor(radius: .widthFraction(0.2)) |> BorderImageProcessor(border: .init(color: .systemBlue.withAlphaComponent(0.7), lineWidth: 12, radius: .widthFraction(0.2))), "Round Border"),
  42. (BlendImageProcessor(blendMode: .lighten, alpha: 1.0, backgroundColor: .red), "Blend"),
  43. (BlurImageProcessor(blurRadius: 5), "Blur"),
  44. (OverlayImageProcessor(overlay: .red, fraction: 0.5), "Overlay"),
  45. (TintImageProcessor(tint: UIColor.red.withAlphaComponent(0.5)), "Tint"),
  46. (ColorControlsProcessor(brightness: 0.0, contrast: 1.1, saturation: 1.1, inputEV: 1.0), "Vibrancy"),
  47. (BlackWhiteProcessor(), "B&W"),
  48. (CroppingImageProcessor(size: CGSize(width: 100, height: 100)), "Cropping"),
  49. (DownsamplingImageProcessor(size: CGSize(width: 25, height: 25)), "Downsampling"),
  50. (BlurImageProcessor(blurRadius: 5) |> RoundCornerImageProcessor(cornerRadius: 20), "Blur + Round Corner")
  51. ]
  52. override func viewDidLoad() {
  53. super.viewDidLoad()
  54. title = "Processor"
  55. setupOperationNavigationBar()
  56. }
  57. override func numberOfSections(in collectionView: UICollectionView) -> Int {
  58. return 1
  59. }
  60. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  61. return ImageLoader.sampleImageURLs.count
  62. }
  63. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  64. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImageCollectionViewCell
  65. let url = ImageLoader.sampleImageURLs[indexPath.row]
  66. KF.url(url)
  67. .setProcessor(currentProcessor)
  68. .serialize(as: .PNG)
  69. .onSuccess { print($0) }
  70. .onFailure { print($0) }
  71. .set(to: cell.cellImageView)
  72. return cell
  73. }
  74. override func alertPopup(_ sender: Any) -> UIAlertController {
  75. let alert = super.alertPopup(sender)
  76. alert.addAction(UIAlertAction(title: "Processor", style: .default, handler: { _ in
  77. let alert = UIAlertController(title: "Processor", message: nil, preferredStyle: .actionSheet)
  78. for item in self.processors {
  79. alert.addAction(UIAlertAction(title: item.1, style: .default) { _ in self.currentProcessor = item.0 })
  80. }
  81. alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
  82. alert.popoverPresentationController?.barButtonItem = sender as? UIBarButtonItem
  83. self.present(alert, animated: true)
  84. }))
  85. return alert
  86. }
  87. }