12345678910111213141516171819202122232425262728293031 |
- //
- // TSPhotoSizeHelper.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/4/6.
- //
- import Photos
- // MARK: - 精准获取图片大小(单位:字节)
- struct TSPhotoSizeHelper {
- /// 同步获取(适合快速判断)
- static func getImageFileSizeSync(asset: PHAsset) -> Int {
- let resources = PHAssetResource.assetResources(for: asset)
- guard let resource = resources.first else { return 0 }
- return Int(resource.value(forKey: "fileSize") as? Int64 ?? 0)
- }
-
- /// 异步获取(推荐主线程使用)
- static func getImageFileSizeAsync(asset: PHAsset, completion: @escaping (Int) -> Void) {
- DispatchQueue.global(qos: .userInitiated).async {
- let size = getImageFileSizeSync(asset: asset)
- DispatchQueue.main.async { completion(size) }
- }
- }
-
- /// 快速判断是否超过10MB
- static func isLargerThan10MB(asset: PHAsset) -> Bool {
- return getImageFileSizeSync(asset: asset) > 10 * 1024 * 1024
- }
- }
|