Working with Images in Swift: resize, crop (iOS/macOS)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Images: resize, crop in Swift (iOS/macOS)



Image processing is one of the key tasks when developing applications for iOS and macOS. Whether it's uploading a user avatar, preparing photo previews, or working with graphical content, you will almost certainly need resize and crop operations. In this article, we will explore how to perform these tasks in Swift using the built-in frameworks UIKit (for iOS) and AppKit (for macOS), as well as the universal Core Graphics.



Main Approaches



In the Apple ecosystem, there are several ways to manipulate images. We will look at two of the most popular and performant methods:



  • UIKit / AppKit + Core Graphics — a low-level but very flexible approach. It allows precise control over the quality and size of the result.
  • Image I/O — a framework for working with metadata and encoding/decoding images. It is well-suited for tasks where performance with large files is important.


In this article, we will focus on the first method, as it is the most universal and does not require additional dependencies.



Resizing an Image (Resize)



The resize function takes the original image and a target size, and returns a new image with the specified parameters. It is important to maintain the aspect ratio to avoid distortion.



Example for iOS (UIKit)



import UIKit

extension UIImage { func resized(to targetSize: CGSize) -> UIImage? { // Determine the scale to fit within targetSize while maintaining aspect ratio let widthRatio = targetSize.width / size.width let heightRatio = targetSize.height / size.height let scale = min(widthRatio, heightRatio) // New size while maintaining aspect ratio let newSize = CGSize(width: size.width * scale, height: size.height * scale) // Create a graphics context with the new size UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) draw(in: CGRect(origin: .zero, size: newSize)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage }}

// Usage:if let image = UIImage(named: "example"), let resized = image.resized(to: CGSize(width: 300, height: 200)) { // Use resized}


Example for macOS (AppKit)



import AppKit

extension NSImage { func resized(to targetSize: NSSize) -> NSImage? { let widthRatio = targetSize.width / size.width let heightRatio = targetSize.height / size.height let scale = min(widthRatio, heightRatio) let newSize = NSSize(width: size.width * scale, height: size.height * scale) let newImage = NSImage(size: newSize) newImage.lockFocus() draw(in: NSRect(origin: .zero, size: newSize), from: NSRect(origin: .zero, size: size), operation: .copy, fraction: 1.0) newImage.unlockFocus() return newImage }}


Important: For macOS, the function UIGraphicsBeginImageContextWithOptions is not available. Use lockFocus()/unlockFocus() or Core Graphics.


Cropping an Image (Crop)



Cropping allows you to extract a rectangular area from the original image. It is most often used for creating square avatars or removing unnecessary edges.



Universal Method Using Core Graphics



import CoreGraphics

extension UIImage { func cropped(to rect: CGRect) -> UIImage? { guard let cgImage = self.cgImage else { ret

Recommendations