ios 获取iCloud中文件的大小

t8e9dugd  于 2023-05-19  发布在  iOS
关注(0)|答案(1)|浏览(115)

有没有办法获取原始文件未下载的filSize属性?我得到了fileURLUIDocumentPickerViewController,然后我试图检查fileSize,但似乎它只是尚未下载的原始文件的别名。尺寸也不对。

debugPrint("size: ", try? fileURL.resourceValues(forKeys: [.fileSizeKey, .ubiquitousItemDownloadingStatusKey]))
"fileURL: " file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Downloads/.vvvvv.mp4.icloud
"size: " Optional(Foundation.URLResourceValues(_values: [__C.NSURLResourceKey(_rawValue: NSURLUbiquitousItemDownloadingStatusKey): NSURLUbiquitousItemDownloadingStatusNotDownloaded, __C.NSURLResourceKey(_rawValue: NSURLFileSizeKey): 340], _keys: Set([__C.NSURLResourceKey(_rawValue: NSURLUbiquitousItemDownloadingStatusKey), __C.NSURLResourceKey(_rawValue: NSURLFileSizeKey)])))
nmpmafwu

nmpmafwu1#

试试这个:

func getFileSize(fileURL: URL?) -> Int64? {
    guard let fileURL,
          fileURL.startAccessingSecurityScopedResource(),
          let fileAttributes = try? FileManager.default.attributesOfItem(atPath: fileURL.path),
          let fileSizeInBytes = fileAttributes[.size] as? Int64 else {
        return nil
    }

    fileURL.stopAccessingSecurityScopedResource()

    return fileSizeInBytes
}

相关问题