 public extension Array where Element == PHAsset {
     func calcSize(completionHandler: @escaping (Int) -> ()) {
         DispatchQueue.global().async {
-            let urls = self.map { $0.url }
+            let urls = self.map { me -> Int in
+                var fileSize = 0
+                autoreleasepool {
+                    fileSize = me.fileSize
+                }
+                return fileSize
+            }
             DispatchQueue.main.async {
-                let size = urls.reduce(0) { p, n in
-                    return p + (n?.fileSize ?? 0)
-                }
-                completionHandler(size)
+                completionHandler(urls.reduce(0, +))
             }
         }
     }

        }
    }
}


}

extension PHAsset {
    var imageFileSize: Int {
        assert(!Thread.current.isMainThread, "Do not call it in main thread!")
        var fileSize = 0
        PhotosSheet.PhotosManager.shared.fetchPhoto(with: self, type: .original, isSynchronous: true) {
            let data = UIImageJPEGRepresentation($0, 1)
            fileSize = data?.count ?? 0
        }
        return fileSize
    }

    var videoFileSize: Int {
        assert(!Thread.current.isMainThread, "Do not call it in main thread!")
        let options = PHVideoRequestOptions()
        options.version = .original
        var ret = 0
        let group = DispatchGroup()
        group.enter()
        PHImageManager.default().requestAVAsset(forVideo: self, options: options) { avAsset, _, _ in
            ret = (avAsset as? AVURLAsset)?.url.fileSize ?? 0
            group.leave()
        }
        group.wait()
        return ret
    }

    var fileSize: Int {
        assert(!Thread.current.isMainThread, "Do not call it in main thread!")
        switch mediaType {
        case .image:
            return imageFileSize
        case .video:
            return videoFileSize
        default:
            return 0
        }
    }
}

extension URL {
    var fileSize: Int {
        guard let resourceValues = try? resourceValues(forKeys: [.fileSizeKey, .totalFileSizeKey]) else { return 0 }
        return resourceValues.fileSize ?? resourceValues.totalFileSize ?? 0
    }
}

extension Int {
    var sizeString: String {
        let bcf = ByteCountFormatter()
        bcf.allowedUnits = [.useBytes, .useKB, .useMB]
        bcf.countStyle = .file
        return bcf.string(fromByteCount: Int64(self))
    }
