Get PHAsset file size without uploading to resource?
Is there a way to get the size of a file on disk PHAsset
without doing requestImageDataForAsset
or converting it to ALAsset
? I download previews of a custom photo library - perhaps many thousands of them - and it is imperative for my application to know the size before importing.
+3
source to share
1 answer
You can grab fileSize
PHAsset and convert it to human readable form:
let resources = PHAssetResource.assetResources(for: yourAsset) // your PHAsset
var sizeOnDisk: Int64? = 0
if let resource = resources.first {
let unsignedInt64 = resource.value(forKey: "fileSize") as? CLong
sizeOnDisk = Int64(bitPattern: UInt64(unsignedInt64!))
}
Then use a variable sizeOnDisk
and pass it to a method like this ...
func converByteToHumanReadable(_ bytes:Int64) -> String {
let formatter:ByteCountFormatter = ByteCountFormatter()
formatter.countStyle = .binary
return formatter.string(fromByteCount: Int64(bytes))
}
+8
source to share