ZLFetchImageOperation.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // ZLFetchImageOperation.swift
  3. // ZLPhotoBrowser
  4. //
  5. // Created by long on 2020/8/18.
  6. //
  7. // Copyright (c) 2020 Long Zhang <495181165@qq.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 Photos
  28. class ZLFetchImageOperation: Operation {
  29. let model: ZLPhotoModel
  30. let isOriginal: Bool
  31. let progress: ( (CGFloat, Error?, UnsafeMutablePointer<ObjCBool>, [AnyHashable : Any]?) -> Void )?
  32. let completion: ( (UIImage?, PHAsset?) -> Void )
  33. var pri_isExecuting = false {
  34. willSet {
  35. self.willChangeValue(forKey: "isExecuting")
  36. }
  37. didSet {
  38. self.didChangeValue(forKey: "isExecuting")
  39. }
  40. }
  41. override var isExecuting: Bool {
  42. return self.pri_isExecuting
  43. }
  44. var pri_isFinished = false {
  45. willSet {
  46. self.willChangeValue(forKey: "isFinished")
  47. }
  48. didSet {
  49. self.didChangeValue(forKey: "isFinished")
  50. }
  51. }
  52. override var isFinished: Bool {
  53. return self.pri_isFinished
  54. }
  55. var pri_isCancelled = false {
  56. willSet {
  57. self.willChangeValue(forKey: "isCancelled")
  58. }
  59. didSet {
  60. self.didChangeValue(forKey: "isCancelled")
  61. }
  62. }
  63. var requestImageID: PHImageRequestID = PHInvalidImageRequestID
  64. override var isCancelled: Bool {
  65. return self.pri_isCancelled
  66. }
  67. init(model: ZLPhotoModel, isOriginal: Bool, progress: ( (CGFloat, Error?, UnsafeMutablePointer<ObjCBool>, [AnyHashable : Any]?) -> Void )? = nil, completion: @escaping ( (UIImage?, PHAsset?) -> Void )) {
  68. self.model = model
  69. self.isOriginal = isOriginal
  70. self.progress = progress
  71. self.completion = completion
  72. super.init()
  73. }
  74. override func start() {
  75. if self.isCancelled {
  76. self.fetchFinish()
  77. return
  78. }
  79. zl_debugPrint("---- start fetch")
  80. self.pri_isExecuting = true
  81. // 存在编辑的图片
  82. if let ei = self.model.editImage {
  83. if ZLPhotoConfiguration.default().saveNewImageAfterEdit {
  84. ZLPhotoManager.saveImageToAlbum(image: ei) { [weak self] (suc, asset) in
  85. self?.completion(ei, asset)
  86. self?.fetchFinish()
  87. }
  88. } else {
  89. DispatchQueue.main.async {
  90. self.completion(ei, nil)
  91. self.fetchFinish()
  92. }
  93. }
  94. return
  95. }
  96. if ZLPhotoConfiguration.default().allowSelectGif, self.model.type == .gif {
  97. self.requestImageID = ZLPhotoManager.fetchOriginalImageData(for: self.model.asset) { [weak self] (data, _, isDegraded) in
  98. if !isDegraded {
  99. let image = UIImage.zl_animateGifImage(data: data)
  100. self?.completion(image, nil)
  101. self?.fetchFinish()
  102. }
  103. }
  104. return
  105. }
  106. if self.isOriginal {
  107. self.requestImageID = ZLPhotoManager.fetchOriginalImage(for: self.model.asset, progress: self.progress) { [weak self] (image, isDegraded) in
  108. if !isDegraded {
  109. zl_debugPrint("---- 下载完成 \(String(describing: self?.isCancelled))")
  110. self?.completion(image?.fixOrientation(), nil)
  111. self?.fetchFinish()
  112. }
  113. }
  114. } else {
  115. self.requestImageID = ZLPhotoManager.fetchImage(for: self.model.asset, size: self.model.previewSize, progress: self.progress) { [weak self] (image, isDegraded) in
  116. if !isDegraded {
  117. self?.completion(self?.scaleImage(image?.fixOrientation()), nil)
  118. self?.fetchFinish()
  119. }
  120. }
  121. }
  122. }
  123. func scaleImage(_ image: UIImage?) -> UIImage? {
  124. guard let i = image else {
  125. return nil
  126. }
  127. guard let data = i.jpegData(compressionQuality: 1) else {
  128. return i
  129. }
  130. let mUnit: CGFloat = 1024 * 1024
  131. if data.count < Int(0.2 * mUnit) {
  132. return i
  133. }
  134. let scale: CGFloat = (data.count > Int(mUnit) ? 0.5 : 0.7)
  135. guard let d = i.jpegData(compressionQuality: scale) else {
  136. return i
  137. }
  138. return UIImage(data: d)
  139. }
  140. func fetchFinish() {
  141. self.pri_isExecuting = false
  142. self.pri_isFinished = true
  143. }
  144. override func cancel() {
  145. super.cancel()
  146. zl_debugPrint("---- cancel \(self.isExecuting) \(self.requestImageID)")
  147. PHImageManager.default().cancelImageRequest(self.requestImageID)
  148. self.pri_isCancelled = true
  149. if self.isExecuting {
  150. self.fetchFinish()
  151. }
  152. }
  153. }