PHAsset+ImageInfo.swift 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // PHAsset+ImageInfo.swift
  3. // Demo
  4. //
  5. // Created by Groot on 2025/4/27.
  6. //
  7. import Foundation
  8. import Photos
  9. // MARK: - PHAsset扩展
  10. extension PHAsset {
  11. /// 从资产中提取图像信息
  12. /// - Returns: 图像信息
  13. func extractAssetInfo() -> ImageItem.ImageInfo? {
  14. let fileSize = self.getSize()
  15. let imageInfo = ImageItem.ImageInfo(
  16. time: self.creationDate,
  17. locationCoordinate: self.location?.coordinate,
  18. fileSize: fileSize,
  19. pixelSize: CGSize(width: self.pixelWidth, height: self.pixelHeight)
  20. )
  21. return imageInfo
  22. }
  23. /// 获取资产文件大小
  24. /// - Returns: 文件大小(字节)
  25. func getSize() -> Int64? {
  26. let resources = PHAssetResource.assetResources(for: self)
  27. guard let resource = resources.first,
  28. let unsignedInt64 = resource.value(forKey: "fileSize") as? CLong else {
  29. return nil
  30. }
  31. return Int64(bitPattern: UInt64(unsignedInt64))
  32. }
  33. }