ImageUtil.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // ImageUtil.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/8/30.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. import CocoaLumberjack
  10. import Promises
  11. extension UIImage {
  12. // public static func base64ToImage(_ source:String,defaultImage:UIImage=UIImage(named: "personDefaultIcon")!) -> UIImage {
  13. // if source != ""{
  14. // let theImage = UIImage(data: Data(base64Encoded:source,options:NSData.Base64DecodingOptions.ignoreUnknownCharacters)!)
  15. // return theImage!
  16. // }else{
  17. // return defaultImage
  18. // }
  19. // }
  20. //
  21. // class func image(color: UIColor, size: CGSize) -> UIImage {
  22. //
  23. // let rect = CGRect.init(x: 0, y: 0, width: size.width, height: size.height)
  24. // UIGraphicsBeginImageContext(rect.size)
  25. //
  26. // let context = UIGraphicsGetCurrentContext()
  27. //
  28. // context?.setFillColor(color.cgColor)
  29. // context?.fill(rect)
  30. //
  31. // let img = UIGraphicsGetImageFromCurrentImageContext()
  32. // UIGraphicsEndImageContext()
  33. //
  34. // return img!
  35. // }
  36. }
  37. class ImageUtil {
  38. private init() { }
  39. static let shared: ImageUtil = {
  40. return ImageUtil()
  41. }()
  42. private let o2ProcessAPI = OOMoyaProvider<OOApplicationAPI>()
  43. //获取流程应用的图标
  44. func getProcessApplicationIcon(id: String) -> Promise<UIImage> {
  45. return Promise { fulfill, reject in
  46. self.loadApplicationBase64FromCache(id: id).then { (image) in
  47. fulfill(image)
  48. }.catch { (error) in
  49. DDLogError("从缓存获取流程应用图标失败 \(error.localizedDescription)")
  50. self.loadApplicationBase64FromNet(id: id).then { (img) in
  51. fulfill(img)
  52. }.catch { (err) in
  53. DDLogError("从网络获取流程应用图标失败 \(err.localizedDescription)")
  54. fulfill(UIImage(named: "todo_8")!)
  55. }
  56. }
  57. }
  58. }
  59. //从缓存文件读取base64 转换成image
  60. private func loadApplicationBase64FromCache(id: String) -> Promise<UIImage> {
  61. return Promise { fulfill, reject in
  62. let icon = self.readBase64String(id: id)
  63. if icon != nil && icon?.isEmpty != true {
  64. fulfill(UIImage.base64ToImage(icon!, defaultImage: UIImage(named: "todo_8")!))
  65. } else {
  66. reject(OOAppError.apiEmptyResultError)
  67. }
  68. }
  69. }
  70. //下载base64 转换成image
  71. private func loadApplicationBase64FromNet(id: String) -> Promise<UIImage> {
  72. return Promise { fulfill, reject in
  73. self.o2ProcessAPI.request(.icon(id), completion: { result in
  74. let myResult = OOResult<BaseModelClass<O2ApplicationIcon>>(result)
  75. if myResult.isResultSuccess() {
  76. if let item = myResult.model?.data, let icon = item.icon, !icon.isEmpty {
  77. DDLogDebug("网络获取icon成功...............")
  78. self.writeBase64StringFile(id: id, base64: icon)//写入缓存
  79. fulfill(UIImage.base64ToImage(icon, defaultImage: UIImage(named: "todo_8")!))
  80. } else {
  81. reject(OOAppError.apiEmptyResultError)
  82. }
  83. } else {
  84. reject(myResult.error!)
  85. }
  86. })
  87. }
  88. }
  89. //从缓存文件读取base64字符串
  90. private func readBase64String(id: String) -> String? {
  91. let path = O2.base64CacheLocalFolder().appendingPathComponent("\(id)")
  92. do {
  93. return try String(contentsOf: path, encoding: .utf8)
  94. }
  95. catch {
  96. DDLogError(error.localizedDescription)
  97. }
  98. return nil
  99. }
  100. //base64字符串写入本地缓存文件 用id作为文件名称
  101. private func writeBase64StringFile(id: String, base64: String) {
  102. let path = O2.base64CacheLocalFolder().appendingPathComponent("\(id)")
  103. do {
  104. try base64.write(to: path, atomically: false, encoding: .utf8)
  105. } catch {
  106. DDLogError(error.localizedDescription)
  107. }
  108. // if FileManager.default.fileExists(atPath: path.path) {
  109. //
  110. // }else {
  111. // let result = FileManager.default.createFile(atPath: path.path, contents: base64.data(using: .utf8), attributes: nil)
  112. // DDLogDebug("创建文件完成, result:\(result)")
  113. // }
  114. }
  115. }