IDN Media Timotius Leonardo Lianoto

How IDN Media Determines Exact Photo Storage Size in Swift

Original post 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

Critical Insight

Using PHAsset with a custom averaging formula gets within 0.01 MB of actual file size, compared to multi-megabyte errors from traditional methods.

Continue reading

What This Article Covers

Problem

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.

Solution

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.

Impact

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.

The article includes the exact Swift code and explains why adding an 'average deficit' calculation makes all the difference.

This page summarizes “Determining Exact Photo Storage Size in Swift” by IDN Media, published Jun 13, 2023. All credit for the original work goes to the authors.

Read the original on engineering.idn.media (opens in new tab)

AI tools for iOS

Browse all AI tools →