KMImageModel.swift 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // KMImageModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/12/5.
  6. //
  7. import Foundation
  8. func moniker(x: Float) -> String {
  9. if x > 1048576 {
  10. return "G"
  11. }else if x >= 1024 {
  12. return "M"
  13. }else{
  14. return "K"
  15. }
  16. }
  17. func truesize(x: Float) -> Float {
  18. if x > 1048576 {
  19. return x / 1048576.0
  20. }else if x >= 1024 {
  21. return x / 1024.0
  22. }else{
  23. return x
  24. }
  25. }
  26. class KMImageModel: NSObject{
  27. var photoPath: String = ""
  28. var photoName: String = ""
  29. var photoSize: String = ""
  30. var photoScale: String = ""
  31. init(filepath: String) {
  32. super.init()
  33. self.photoPath = filepath
  34. let attributesDic = try? FileManager.default.attributesOfItem(atPath: filepath)
  35. let fileSize = attributesDic?[FileAttributeKey.size] as? Float ?? 0
  36. let size = fileSize/1024.0
  37. self.photoSize = String(format: "%.1f %@", truesize(x: size), moniker(x: size))
  38. self.photoName = filepath.lastPathComponent
  39. let img = NSImage(contentsOfFile: filepath)
  40. self.photoScale = String(format: "%.0f x %.0f", img?.size.width ?? 0, img?.size.height ?? 0)
  41. }
  42. }