| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- //
- // PHAsset+ImageInfo.swift
- // Demo
- //
- // Created by Groot on 2025/4/27.
- //
- import Foundation
- import Photos
- // MARK: - PHAsset扩展
- extension PHAsset {
- /// 从资产中提取图像信息
- /// - Returns: 图像信息
- func extractAssetInfo() -> ImageItem.ImageInfo? {
- let fileSize = self.getSize()
-
- let imageInfo = ImageItem.ImageInfo(
- time: self.creationDate,
- locationCoordinate: self.location?.coordinate,
- fileSize: fileSize,
- pixelSize: CGSize(width: self.pixelWidth, height: self.pixelHeight)
- )
- return imageInfo
- }
-
- /// 获取资产文件大小
- /// - Returns: 文件大小(字节)
- func getSize() -> Int64? {
- let resources = PHAssetResource.assetResources(for: self)
-
- guard let resource = resources.first,
- let unsignedInt64 = resource.value(forKey: "fileSize") as? CLong else {
- return nil
- }
-
- return Int64(bitPattern: UInt64(unsignedInt64))
- }
- }
|