Suppose there is a PHAsset representing image. How to get file size of the image?
The only way I see is using valueForKey:
with private ALAssetURL
key, and retrieving size of ALAssetRepresentation
, but apple can reject such app.
Swift 4/5 solution
Extension
extension PHAsset {
var fileSize: Float {
get {
let resource = PHAssetResource.assetResources(for: self)
let imageSizeByte = resource.first?.value(forKey: "fileSize") as! Float
let imageSizeMB = imageSizeByte / (1024.0*1024.0)
return imageSizeMB
}
}
}
Function
func getImageFileSize(from asset: PHAsset) -> Float{
let resource = PHAssetResource.assetResources(for: asset)
let imageSizeByte = resource.first?.value(forKey: "fileSize") as! Float
let imageSizeMB = imageSizeByte / (1024.0*1024.0)
return imageSizeMB
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With