ZLPhotoModel.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // ZLPhotoModel.swift
  3. // ZLPhotoBrowser
  4. //
  5. // Created by long on 2020/8/11.
  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. extension ZLPhotoModel {
  29. public enum MediaType: Int {
  30. case unknown = 0
  31. case image
  32. case gif
  33. case livePhoto
  34. case video
  35. }
  36. }
  37. public class ZLPhotoModel: NSObject {
  38. public let ident: String
  39. public let asset: PHAsset
  40. public var type: ZLPhotoModel.MediaType = .unknown
  41. public var duration: String = ""
  42. public var isSelected: Bool = false
  43. private var pri_editImage: UIImage? = nil
  44. public var editImage: UIImage? {
  45. set {
  46. pri_editImage = newValue
  47. }
  48. get {
  49. if let _ = self.editImageModel {
  50. return pri_editImage
  51. } else {
  52. return nil
  53. }
  54. }
  55. }
  56. public var second: Second {
  57. guard type == .video else {
  58. return 0
  59. }
  60. return Int(round(asset.duration))
  61. }
  62. public var whRatio: CGFloat {
  63. return CGFloat(self.asset.pixelWidth) / CGFloat(self.asset.pixelHeight)
  64. }
  65. public var previewSize: CGSize {
  66. let scale: CGFloat = 2 //UIScreen.main.scale
  67. if self.whRatio > 1 {
  68. let h = min(UIScreen.main.bounds.height, ZLMaxImageWidth) * scale
  69. let w = h * self.whRatio
  70. return CGSize(width: w, height: h)
  71. } else {
  72. let w = min(UIScreen.main.bounds.width, ZLMaxImageWidth) * scale
  73. let h = w / self.whRatio
  74. return CGSize(width: w, height: h)
  75. }
  76. }
  77. // Content of the last edit.
  78. public var editImageModel: ZLEditImageModel?
  79. public init(asset: PHAsset) {
  80. self.ident = asset.localIdentifier
  81. self.asset = asset
  82. super.init()
  83. self.type = self.transformAssetType(for: asset)
  84. if self.type == .video {
  85. self.duration = self.transformDuration(for: asset)
  86. }
  87. }
  88. public func transformAssetType(for asset: PHAsset) -> ZLPhotoModel.MediaType {
  89. switch asset.mediaType {
  90. case .video:
  91. return .video
  92. case .image:
  93. if (asset.value(forKey: "filename") as? String)?.hasSuffix("GIF") == true {
  94. return .gif
  95. }
  96. if #available(iOS 9.1, *) {
  97. if asset.mediaSubtypes == .photoLive || asset.mediaSubtypes.rawValue == 10 {
  98. return .livePhoto
  99. }
  100. }
  101. return .image
  102. default:
  103. return .unknown
  104. }
  105. }
  106. public func transformDuration(for asset: PHAsset) -> String {
  107. let dur = Int(round(asset.duration))
  108. switch dur {
  109. case 0..<60:
  110. return String(format: "00:%02d", dur)
  111. case 60..<3600:
  112. let m = dur / 60
  113. let s = dur % 60
  114. return String(format: "%02d:%02d", m, s)
  115. case 3600...:
  116. let h = dur / 3600
  117. let m = (dur % 3600) / 60
  118. let s = dur % 60
  119. return String(format: "%02d:%02d:%02d", h, m, s)
  120. default:
  121. return ""
  122. }
  123. }
  124. }
  125. public func ==(lhs: ZLPhotoModel, rhs: ZLPhotoModel) -> Bool {
  126. return lhs.ident == rhs.ident
  127. }