Working with Images in Swift (iOS/macOS)
In this article, we will take a detailed look at working with images in Swift for iOS and macOS platforms. You will learn how to load, display, process, and save images using Apple's built-in frameworks: UIKit (iOS), AppKit (macOS), and Core Image. The material will be useful for both beginners and experienced developers.
Main Frameworks for Working with Images
In the Apple ecosystem, there are several ways to work with images. The main ones are:
- UIKit (iOS/tvOS) —
UIImage,UIImageView - AppKit (macOS) —
NSImage,NSImageView - Core Image — a powerful framework for image processing (filters, transformations)
- SwiftUI —
Imagefor declarative display
Loading Images in iOS (UIKit)
The simplest way to load an image in iOS is to use UIImage. Example of loading from assets and from a file:
// Loading from Assets.xcassetslet imageFromAssets = UIImage(named: "myImage")
// Loading from a file in Bundleif let path = Bundle.main.path(forResource: "photo", ofType: "jpg") { let imageFromFile = UIImage(contentsOfFile: path)}
// Loading from Dataif let data = try? Data(contentsOf: URL(string: "https://example.com/image.png")!) { let imageFromData = UIImage(data: data)}Displaying Images in UIKit
To display an image, UIImageView is used:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))imageView.image = UIImage(named: "example")imageView.contentMode = .scaleAspectFitview.addSubview(imageView)Working with Images in macOS (AppKit)
In macOS, NSImage and NSImageView are used:
// Loading an imagelet nsImage = NSImage(named: "macImage")
// Displayinglet imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 300, height: 200))imageView.image = nsImageimageView.imageScaling = .scaleProportionallyUpOrDownview.addSubview(imageView)Image Processing with Core Image
Core Image provides dozens of built-in filters. Let's look at basic operations:
import CoreImage
// Create a CIImage from UIImageguard let uiImage = UIImage(named: "input"), let ciImage = CIImage(image: uiImage) else { return }
// Apply the "Gaussian Blur" filterlet filter = CIFilter(name: "CIGaussianBlur")filter?.setValue(ciImage, forKey: kCIInputImageKey)filter?.setValue(10.0, forKey: kCIInputRadiusKey)
// Get the resultguard let outputImage = filter?.outputImage else { return }
// Convert back to UIImagelet context = CIContext(options: nil)if let cgImage = context.createCGImage(outputImage, from: outputImage.extent) { let blurredImage = UIImage(cgImage: cgImage)}Popular Core Image Filters
CISepiaTone— sepia toneCIPhotoEffectMono— monochrome effectCIVignette— vignettingCIColorControls— brightness, contrast, saturationCICrop— image cropping
Working with Images in SwiftUI
SwiftUI provides a simple and declarative way to work with images:
import SwiftUI
struct ContentView: View { var body: some View { Image("swiftui_image") .resizable() .scaledToFit() .frame(width: 200, height: 200) .clipShape(Circle()) .overlay(Circle().stroke(Color.blue, lineWidth: 4)) .shadow(radius: 10) }}