How to compress of reduce the size of an image before uploading to Parse as PFFile? Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reducing image size before upload is usually a mix of resizing pixel dimensions and compressing the encoded data. If you skip both and upload the full camera image directly to Parse, you pay for it in upload time, bandwidth, memory, and storage.
Resize First, Then Compress
Compression quality alone is often not enough. A huge 4032x3024 photo compressed at moderate JPEG quality can still be much larger than the app actually needs.
A practical workflow is:
- resize the image to a reasonable display or storage size
- encode it as JPEG with a chosen compression quality
- upload the resulting data as
PFFileObjector the legacyPFFile
That order matters because resizing removes pixels, while JPEG quality only changes how those pixels are encoded.
Resize with UIGraphicsImageRenderer
A modern way to scale a UIImage is UIGraphicsImageRenderer.
This keeps the aspect ratio intact and avoids upscaling images that are already smaller than the limit.
Compress to JPEG Data
After resizing, turn the image into compressed JPEG data.
Typical quality values are between 0.5 and 0.8 for user-uploaded app images. The right value depends on the visual quality you need and the network conditions your users face.
Put It Together Before Upload
Here is the full flow:
If you are using an older Parse SDK that still uses PFFile, the compression part stays the same. Only the file object type changes.
Upload Example
Once you have the file object, attach it to a Parse object and save it.
This is the same upload shape you would use without compression, but now the payload is much more reasonable.
Add a Size Target if Needed
Sometimes you need a file below a specific size rather than just “smaller.” In that case, iterate compression quality downward until the data fits.
In practice, combine this with resizing first. Trying to hit strict file-size limits using compression alone often destroys quality unnecessarily.
Why PNG Is Usually the Wrong Choice for Photos
PNG is lossless and great for flat graphics, screenshots, and images with transparency. For camera photos, JPEG is usually the better format because it compresses photographic content much more efficiently.
If you upload a large photo as PNG, the result is often dramatically larger than it needs to be.
Common Pitfalls
The biggest mistake is compressing without resizing. Camera images are often far larger in pixel dimensions than the app actually needs.
Another mistake is uploading PNG for ordinary photographs. That often wastes bandwidth and storage.
Developers also sometimes compress aggressively without testing the visual result. A file that is small but visibly degraded is not a good user experience.
Finally, large image handling can use a lot of memory. If you work with full-resolution camera images, do not ignore memory pressure while resizing and encoding.
Summary
- Reduce image upload size by resizing dimensions first and compressing second.
- '
UIGraphicsImageRendereris a good modern way to resize aUIImage.' - Use
jpegData(compressionQuality:)for photo-style uploads. - Wrap the resulting data in
PFFileObjector the legacyPFFilebefore saving to Parse. - If you need a strict file-size limit, iterate compression quality after resizing.

