O2SlideImageAFSource.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // O2SlideImageAFSource.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/10/29.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import ImageSlideshow
  9. import Alamofire
  10. import AlamofireImage
  11. import CocoaLumberjack
  12. public class O2SlideImageAFSource: NSObject, InputSource {
  13. var url: URL
  14. public init(url: URL) {
  15. self.url = url
  16. super.init()
  17. }
  18. public init?(urlString: String) {
  19. if let validUrl = URL(string: urlString) {
  20. self.url = validUrl
  21. super.init()
  22. } else {
  23. return nil
  24. }
  25. }
  26. public func load(to imageView: UIImageView, with callback: @escaping (_ image: UIImage?) -> Void){
  27. let config = ImageDownloader.defaultURLSessionConfiguration()
  28. let imageDownloader = ImageDownloader(configuration: config,
  29. downloadPrioritization: .fifo,
  30. maximumActiveDownloads: 10,
  31. imageCache: AutoPurgingImageCache())
  32. //ImageFilter
  33. let imageFilter = AspectScaledToFillSizeFilter(size: CGSize(width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
  34. //placeholderImage
  35. imageView.af_imageDownloader = imageDownloader
  36. var urlRequest = URLRequest(url: self.url)
  37. urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
  38. imageView.af_setImage(withURLRequest: urlRequest, placeholderImage: UIImage(named: "file_unknown_icon"), filter: imageFilter, progress: nil, progressQueue: DispatchQueue.main, imageTransition: .crossDissolve(0.1), runImageTransitionIfCached: false) { (response) in
  39. print(response)
  40. switch response.result {
  41. case .success(let image):
  42. imageView.image = image
  43. callback(image)
  44. case .failure(let err):
  45. DDLogError(err.localizedDescription)
  46. }
  47. // if let value = response.result.value {
  48. // let newImage = value
  49. // imageView.image = newImage
  50. // callback(newImage)
  51. // }
  52. }
  53. }
  54. }