Swift
Image Compression
PFFile
Parse
iOS Development

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:

  1. resize the image to a reasonable display or storage size
  2. encode it as JPEG with a chosen compression quality
  3. upload the resulting data as PFFileObject or the legacy PFFile

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.

swift
1import UIKit
2
3func resizedImage(_ image: UIImage, maxDimension: CGFloat) -> UIImage {
4    let originalSize = image.size
5    let scale = min(maxDimension / originalSize.width, maxDimension / originalSize.height, 1)
6    let newSize = CGSize(width: originalSize.width * scale, height: originalSize.height * scale)
7
8    let renderer = UIGraphicsImageRenderer(size: newSize)
9    return renderer.image { _ in
10        image.draw(in: CGRect(origin: .zero, size: newSize))
11    }
12}

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.

swift
1import UIKit
2
3func compressedJPEGData(from image: UIImage, quality: CGFloat) -> Data? {
4    return image.jpegData(compressionQuality: quality)
5}

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:

swift
1import UIKit
2import Parse
3
4func makeParseFile(from image: UIImage) -> PFFileObject? {
5    let resized = resizedImage(image, maxDimension: 1280)
6
7    guard let data = resized.jpegData(compressionQuality: 0.7) else {
8        return nil
9    }
10
11    return PFFileObject(name: "photo.jpg", data: data)
12}

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.

swift
1import Parse
2
3let photoObject = PFObject(className: "Photo")
4
5if let file = makeParseFile(from: image) {
6    photoObject["image"] = file
7    photoObject.saveInBackground { success, error in
8        if success {
9            print("upload complete")
10        } else {
11            print(error?.localizedDescription ?? "unknown error")
12        }
13    }
14}

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.

swift
1import UIKit
2
3func jpegDataUnderLimit(from image: UIImage, maxBytes: Int) -> Data? {
4    var quality: CGFloat = 0.9
5
6    while quality >= 0.1 {
7        if let data = image.jpegData(compressionQuality: quality), data.count <= maxBytes {
8            return data
9        }
10        quality -= 0.1
11    }
12
13    return nil
14}

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.
  • 'UIGraphicsImageRenderer is a good modern way to resize a UIImage.'
  • Use jpegData(compressionQuality:) for photo-style uploads.
  • Wrap the resulting data in PFFileObject or the legacy PFFile before saving to Parse.
  • If you need a strict file-size limit, iterate compression quality after resizing.

Course illustration
Course illustration

All Rights Reserved.