Determining Exact Photo Storage Size in Swift
Article Summary
Timotius Leonardo Lianoto from IDN Engineering discovered his iOS app was reporting 13 MB for a 2.8 MB photo. The culprit? A common mistake thousands of developers make when calculating image file sizes.
Most iOS developers rely on data.count or FileManager to calculate photo sizes before upload. But these methods include metadata and thumbnails, creating massive discrepancies between what users see in their Photos app and what your app reports. This causes real friction when users hit upload limits that don't make sense.
Key Takeaways
- Standard data.count methods can show 13 MB for actual 2.8 MB photos
- PHAsset with custom calculation achieves exact size in 14 of 22 test photos
- Remaining photos miss by only 0.01 MB, not multiple megabytes
- Solution avoids quality reduction that degrades user experience
Using PHAsset with a custom averaging formula gets within 0.01 MB of actual file size, compared to multi-megabyte errors from traditional methods.
About This Article
iOS developers often use standard methods like data.count() without realizing they're including embedded metadata and thumbnail images in their calculations. This leads to confusing upload rejection messages even when users' actual files are well under the app's stated limits.
Timotius Leonardo Lianoto built a PHAsset-based extension that pulls the actual file size from PHAssetResource and uses an averaging formula to account for system overhead: (imageSizeMB + (imageSizeMB + imageSizeMB/10)) / 2.
The PHAsset approach matched the Photos app's displayed file size in 14 of 22 test photos. For the remaining photos, the calculations were off by only 0.001 to 0.01 MB on files below 1 MB. This eliminates the multi-megabyte gaps that used to frustrate users.