PhotosUI for Swift: Complete Guide with Examples

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

PhotosUI for Swift: Complete Guide to Picking Photos and Videos

1. What is PhotosUI and Why You Need It

PhotosUI is an Apple framework that provides ready-made user interfaces for working with the device's media library. It's part of UIKit and sits on top of the lower-level Photos framework. The main goal of PhotosUI is to let developers quickly and consistently implement photo and video selection functionality without diving into complex access control logic, album navigation, or thumbnail display.

Why use it? First, it saves time. Instead of building your own media browser from scratch, you get a polished, familiar interface that users already know. Second, it ensures security and compliance with Apple's guidelines. PhotosUI automatically handles library access requests (via PHPhotoLibrary) and works correctly with new iOS versions, including privacy and limited access support. Third, it's flexible: you can customize the appearance, allow multiple selection, filter only photos or only videos, and even configure editing behavior.

Common use cases: profile avatars, attaching images to messages, uploading videos to social networks, creating collages, integrating with filters and editors. PhotosUI is perfect for any app that needs access to the user's media library.

2. Installation

PhotosUI is part of the standard iOS SDK. No installation via CocoaPods, Swift Package Manager, or Carthage is required. All you need to do is import the framework into your project.

Important: PhotosUI is available from iOS 8.0 onwards. However, to use all modern features (like PHPickerViewController), iOS 14+ is recommended.

// Import the framework
import PhotosUI

No additional terminal commands are needed. Simply add import PhotosUI to the file where you plan to use media selection functionality.

3. Quick Start: Minimal Working Example

The easiest way to show users a photo selection interface is to use PHPickerViewController (iOS 14+). This controller runs in a separate process, boosting performance and security.

Example: a button on screen that opens the picker when tapped.

import UIKit
import PhotosUI

class ViewController: UIViewController, PHPickerViewControllerDelegate {

    @IBAction func selectPhotoTapped(_ sender: UIButton) {
        // 1. Create picker configuration
        var config = PHPickerConfiguration()
        config.selectionLimit = 1 // One photo
        config.filter = .images // Images only

        // 2. Create the controller
        let picker = PHPickerViewController(configuration: config)
        picker.delegate = self

        // 3. Present it
        present(picker, animated: true)
    }

    // MARK: - PHPickerViewControllerDelegate
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        dismiss(animated: true)

        guard let provider = results.first?.itemProvider else { return }

        // 4. Load UIImage
        if provider.canLoadObject(ofClass: UIImage.self) {
            provider.loadObject(ofClass: UIImage.self) { image, error in
                DispatchQueue.main.async {
                    // Use the loaded image
                    print("Image loaded: \(String(describing: image))")
                }
            }
        }
    }
}                    

Recommendations