获取位于iCloud中的文件大小。

4

有没有方法可以获取尚未下载的原始文件的fileSize属性? 我使用UIDocumentPickerViewController获取了fileURL,然后尝试检查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)])))
1个回答

0

试试这个:

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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接