O2AlamofireSource.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // O2Alam.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2016/12/16.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import ImageSlideshow
  9. import Alamofire
  10. import AlamofireImage
  11. import CocoaLumberjack
  12. public class O2AlamofireSource: NSObject, InputSource {
  13. var url: URL
  14. public init(url: URL) {
  15. self.url = url
  16. super.init()
  17. }
  18. // var imageHeight:CGFloat {
  19. // get {
  20. // var tmp = CGFloat(142.0)
  21. // let deviceName = UIDevice.deviceModelReadable()
  22. // switch deviceName {
  23. // case "iPhone 5","iPhone 5C","iPhone 5S","iPhone SE":
  24. // tmp = CGFloat(142.0)
  25. // case "iPhone 6","iPhone 6S","iPhone 7":
  26. // tmp = CGFloat(172.0)
  27. // case "iPhone 6 Plus","iPhone 6S Plus","iPhone 7 Plus":
  28. // tmp = CGFloat(202.0)
  29. // default:
  30. // tmp = CGFloat(142.0)
  31. // }
  32. // return tmp
  33. // }
  34. // }
  35. public init?(urlString: String) {
  36. if let validUrl = URL(string: urlString) {
  37. self.url = validUrl
  38. super.init()
  39. } else {
  40. return nil
  41. }
  42. }
  43. public func load(to imageView: UIImageView, with callback: @escaping (_ image: UIImage?) -> Void){
  44. // let frame = imageView.bounds
  45. // if frame.width <= 0 || frame.height <= 0 {
  46. // let height = SCREEN_WIDTH / 2
  47. // imageView.bounds = CGRect(x: 0, y: 0, w: SCREEN_WIDTH, h: height)
  48. // }
  49. // imageView.hnk_setImageFromURL(self.url, placeholder: UIImage(named: "pic_lunbo_1"), format: nil, failure: { (error) in
  50. // DDLogError("下载图片异常\(String(describing: error))")
  51. // callback(nil)
  52. // }) { (newImage) in
  53. // imageView.image = newImage
  54. // callback(newImage)
  55. // }
  56. let config = ImageDownloader.defaultURLSessionConfiguration()
  57. let imageDownloader = ImageDownloader(configuration: config,
  58. downloadPrioritization: .fifo,
  59. maximumActiveDownloads: 10,
  60. imageCache: AutoPurgingImageCache())
  61. //ImageFilter
  62. let height = SCREEN_WIDTH / 2
  63. let imageFilter = ScaledToSizeFilter(size: CGSize(width: SCREEN_WIDTH, height: height))
  64. //placeholderImage
  65. imageView.af_imageDownloader = imageDownloader
  66. // let tempURL = URL(string: "http://d.ifengimg.com/mw978_mh598/p0.ifengimg.com/cmpp/2018/03/21/06/530de4b1-3127-4a9b-b764-efa3444d04e0_size275_w1024_h768.jpg")!
  67. var urlRequest = URLRequest(url: self.url)
  68. urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
  69. imageView.af_setImage(withURLRequest: urlRequest, placeholderImage: UIImage(named: "pic_lunbo_1"), filter: imageFilter, progress: nil, progressQueue: DispatchQueue.main, imageTransition: .crossDissolve(0.1), runImageTransitionIfCached: false) { (response) in
  70. print(response)
  71. if let value = response.data {
  72. let newImage = UIImage(data: value)
  73. imageView.image = newImage
  74. callback(newImage)
  75. }
  76. // if let value = response.result.value {
  77. // let newImage = value
  78. // // let newImage = UIImage.scaleTo(image: value, w: SCREEN_WIDTH , h: self.imageHeight)
  79. // imageView.image = newImage
  80. // callback(newImage)
  81. // }
  82. }
  83. }
  84. // public func load(to imageView: UIImageView, with callback: @escaping (UIImage) -> ()) {
  85. //
  86. // let config = ImageDownloader.defaultURLSessionConfiguration()
  87. // let imageDownloader = ImageDownloader(configuration: config,
  88. // downloadPrioritization: .fifo,
  89. // maximumActiveDownloads: 10,
  90. // imageCache: AutoPurgingImageCache())
  91. // //ImageFilter
  92. // let imageFilter = ScaledToSizeFilter(size: CGSize(width:SCREEN_WIDTH,height:self.imageHeight))
  93. // //placeholderImage
  94. //
  95. // imageView.af_imageDownloader = imageDownloader
  96. //// let tempURL = URL(string: "http://d.ifengimg.com/mw978_mh598/p0.ifengimg.com/cmpp/2018/03/21/06/530de4b1-3127-4a9b-b764-efa3444d04e0_size275_w1024_h768.jpg")!
  97. // var urlRequest = URLRequest(url: self.url)
  98. // urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
  99. // imageView.af_setImage(withURLRequest: urlRequest, placeholderImage: UIImage(named: "pic_lunbo_1"), filter: imageFilter, progress: nil, progressQueue: DispatchQueue.main, imageTransition: .crossDissolve(0.1), runImageTransitionIfCached: false) { (response) in
  100. // if let value = response.result.value {
  101. // let newImage = value
  102. // // let newImage = UIImage.scaleTo(image: value, w: SCREEN_WIDTH , h: self.imageHeight)
  103. // imageView.image = newImage
  104. // callback(newImage)
  105. // }
  106. // }
  107. // }
  108. }