Loading...
Loading...
PHPicker, PhotosPicker, photo selection, limited library access, presentLimitedLibraryPicker, save to camera roll, PHPhotoLibrary, PHAssetCreationRequest, Transferable, PhotosPickerItem, photo permissions
npx skill4agent add charleswiltgen/axiom axiom-photo-library.limited.authorized.limitedWhat do you need?
┌─ User picks photos (no library browsing)?
│ ├─ SwiftUI app → PhotosPicker (iOS 16+)
│ └─ UIKit app → PHPickerViewController (iOS 14+)
│ └─ NO library permission needed! Picker handles it.
│
├─ Display user's full photo library (gallery UI)?
│ └─ Requires PHPhotoLibrary authorization
│ └─ Request .readWrite for browsing
│ └─ Handle .limited status with presentLimitedLibraryPicker
│
├─ Save photos to camera roll?
│ └─ Requires PHPhotoLibrary authorization
│ └─ Request .addOnly (minimal) or .readWrite
│
└─ Just capture with camera?
└─ Don't use PhotoKit - see camera-capture skill| Level | What It Allows | Request Method |
|---|---|---|
| No permission | User picks via system picker | PHPicker/PhotosPicker (automatic) |
| Save to camera roll only | |
| User-selected subset only | User chooses in system UI |
| Full library access | |
<!-- Required for any PhotoKit access -->
<key>NSPhotoLibraryUsageDescription</key>
<string>Access your photos to share them</string>
<!-- Required if saving photos -->
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Save photos to your library</string>import SwiftUI
import PhotosUI
struct ContentView: View {
@State private var selectedItem: PhotosPickerItem?
@State private var selectedImage: Image?
var body: some View {
VStack {
PhotosPicker(
selection: $selectedItem,
matching: .images // Filter to images only
) {
Label("Select Photo", systemImage: "photo")
}
if let image = selectedImage {
image
.resizable()
.scaledToFit()
}
}
.onChange(of: selectedItem) { _, newItem in
Task {
await loadImage(from: newItem)
}
}
}
private func loadImage(from item: PhotosPickerItem?) async {
guard let item else {
selectedImage = nil
return
}
// Load as Data first (more reliable than Image)
if let data = try? await item.loadTransferable(type: Data.self),
let uiImage = UIImage(data: data) {
selectedImage = Image(uiImage: uiImage)
}
}
}@State private var selectedItems: [PhotosPickerItem] = []
PhotosPicker(
selection: $selectedItems,
maxSelectionCount: 5,
matching: .images
) {
Text("Select Photos")
}// Screenshots only
matching: .screenshots
// Screen recordings only
matching: .screenRecordings
// Slo-mo videos
matching: .sloMoVideos
// Cinematic videos (iOS 16+)
matching: .cinematicVideos
// Depth effect photos
matching: .depthEffectPhotos
// Bursts
matching: .bursts
// Compound filters with .any, .all, .not
// Videos AND Live Photos
matching: .any(of: [.videos, .livePhotos])
// All images EXCEPT screenshots
matching: .all(of: [.images, .not(.screenshots)])
// All images EXCEPT screenshots AND panoramas
matching: .all(of: [.images, .not(.any(of: [.screenshots, .panoramas]))])import SwiftUI
import PhotosUI
struct EmbeddedPickerView: View {
@State private var selectedItems: [PhotosPickerItem] = []
var body: some View {
VStack {
// Your content above picker
SelectedPhotosGrid(items: selectedItems)
// Embedded picker fills available space
PhotosPicker(
selection: $selectedItems,
maxSelectionCount: 10,
selectionBehavior: .continuous, // Live updates as user taps
matching: .images
) {
// Label is ignored for inline style
Text("Select")
}
.photosPickerStyle(.inline) // Embed instead of present
.photosPickerDisabledCapabilities([.selectionActions]) // Hide Add/Cancel buttons
.photosPickerAccessoryVisibility(.hidden, edges: .all) // Hide nav/toolbar
.frame(height: 300) // Control picker height
.ignoresSafeArea(.container, edges: .bottom) // Extend to bottom edge
}
}
}| Style | Description |
|---|---|
| Default modal sheet |
| Embedded in your view hierarchy |
| Single row, minimal vertical space |
// Hide navigation/toolbar accessories
.photosPickerAccessoryVisibility(.hidden, edges: .all)
.photosPickerAccessoryVisibility(.hidden, edges: .top) // Just navigation bar
.photosPickerAccessoryVisibility(.hidden, edges: .bottom) // Just toolbar
// Disable capabilities (hides UI for them)
.photosPickerDisabledCapabilities([.search]) // Hide search
.photosPickerDisabledCapabilities([.collectionNavigation]) // Hide albums
.photosPickerDisabledCapabilities([.stagingArea]) // Hide selection review
.photosPickerDisabledCapabilities([.selectionActions]) // Hide Add/Cancel
// Continuous selection for live updates
selectionBehavior: .continuousimport PhotosUI
class PhotoPickerViewController: UIViewController, PHPickerViewControllerDelegate {
func showPicker() {
var config = PHPickerConfiguration()
config.selectionLimit = 1 // 0 = unlimited
config.filter = .images // or .videos, .any(of: [.images, .videos])
let picker = PHPickerViewController(configuration: config)
picker.delegate = self
present(picker, animated: true)
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
guard let result = results.first else { return }
// Load image asynchronously
result.itemProvider.loadObject(ofClass: UIImage.self) { [weak self] object, error in
guard let image = object as? UIImage else { return }
DispatchQueue.main.async {
self?.displayImage(image)
}
}
}
}// Images only
config.filter = .images
// Videos only
config.filter = .videos
// Live Photos only
config.filter = .livePhotos
// Images and videos
config.filter = .any(of: [.images, .videos])
// Exclude screenshots (iOS 15+)
config.filter = .all(of: [.images, .not(.screenshots)])
// iOS 16+ filters
config.filter = .cinematicVideos
config.filter = .depthEffectPhotos
config.filter = .bursts// Configure for embedded use
var config = PHPickerConfiguration()
config.selection = .continuous // Live updates instead of waiting for Add button
config.mode = .compact // Single row layout (optional)
config.selectionLimit = 10
// Hide accessories
config.edgesWithoutContentMargins = .all // No margins around picker
// Disable capabilities
config.disabledCapabilities = [.search, .selectionActions]
let picker = PHPickerViewController(configuration: config)
picker.delegate = self
// Add as child view controller (required for embedded)
addChild(picker)
containerView.addSubview(picker.view)
picker.view.frame = containerView.bounds
picker.didMove(toParent: self)// Deselect assets by their identifiers
picker.deselectAssets(withIdentifiers: ["assetID1", "assetID2"])
// Reorder assets in selection
picker.moveAsset(withIdentifier: "assetID", afterAssetWithIdentifier: "otherID")// SwiftUI - Use .current encoding to preserve HDR
PhotosPicker(
selection: $selectedItems,
matching: .images,
preferredItemEncoding: .current // Don't transcode
) { ... }
// Loading with original format preservation
struct HDRImage: Transferable {
let data: Data
static var transferRepresentation: some TransferRepresentation {
DataRepresentation(importedContentType: .image) { data in
HDRImage(data: data)
}
}
}
// Request .image content type (generic) not .jpeg (specific)
let result = try await item.loadTransferable(type: HDRImage.self)var config = PHPickerConfiguration()
config.preferredAssetRepresentationMode = .current // Don't transcode.limited<!-- Info.plist - Add this to handle limited access UI yourself -->
<key>PHPhotoLibraryPreventAutomaticLimitedAccessAlert</key>
<true/>import Photos
class PhotoLibraryManager {
func checkAndRequestAccess() async -> PHAuthorizationStatus {
let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
switch status {
case .notDetermined:
return await PHPhotoLibrary.requestAuthorization(for: .readWrite)
case .limited:
// User granted limited access - show UI to expand
await presentLimitedLibraryPicker()
return .limited
case .authorized:
return .authorized
case .denied, .restricted:
return status
@unknown default:
return status
}
}
@MainActor
func presentLimitedLibraryPicker() {
guard let windowScene = UIApplication.shared.connectedScenes
.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene,
let rootVC = windowScene.windows.first?.rootViewController else {
return
}
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: rootVC)
}
}// Register for changes
PHPhotoLibrary.shared().register(self)
// In delegate
func photoLibraryDidChange(_ changeInstance: PHChange) {
// User may have modified their limited selection
// Refresh your photo grid
}import Photos
func saveImageToLibrary(_ image: UIImage) async throws {
// Request add-only permission (minimal access)
let status = await PHPhotoLibrary.requestAuthorization(for: .addOnly)
guard status == .authorized || status == .limited else {
throw PhotoError.permissionDenied
}
try await PHPhotoLibrary.shared().performChanges {
PHAssetCreationRequest.creationRequestForAsset(from: image)
}
}
// With metadata preservation
func savePhotoData(_ data: Data, metadata: [String: Any]? = nil) async throws {
try await PHPhotoLibrary.shared().performChanges {
let request = PHAssetCreationRequest.forAsset()
// Write data to temp file for addResource
let tempURL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString)
.appendingPathExtension("jpg")
try? data.write(to: tempURL)
request.addResource(with: .photo, fileURL: tempURL, options: nil)
}
}Image// Custom Transferable for any image format
struct TransferableImage: Transferable {
let image: UIImage
static var transferRepresentation: some TransferRepresentation {
DataRepresentation(importedContentType: .image) { data in
guard let image = UIImage(data: data) else {
throw TransferError.importFailed
}
return TransferableImage(image: image)
}
}
enum TransferError: Error {
case importFailed
}
}
// Usage
func loadImage(from item: PhotosPickerItem) async -> UIImage? {
do {
let result = try await item.loadTransferable(type: TransferableImage.self)
return result?.image
} catch {
print("Failed to load image: \(error)")
return nil
}
}func loadImageWithProgress(from item: PhotosPickerItem) async -> UIImage? {
let progress = Progress()
return await withCheckedContinuation { continuation in
_ = item.loadTransferable(type: TransferableImage.self) { result in
switch result {
case .success(let transferable):
continuation.resume(returning: transferable?.image)
case .failure:
continuation.resume(returning: nil)
}
}
}
}import Photos
class PhotoGalleryViewModel: NSObject, ObservableObject, PHPhotoLibraryChangeObserver {
@Published var photos: [PHAsset] = []
private var fetchResult: PHFetchResult<PHAsset>?
override init() {
super.init()
PHPhotoLibrary.shared().register(self)
fetchPhotos()
}
deinit {
PHPhotoLibrary.shared().unregisterChangeObserver(self)
}
func fetchPhotos() {
let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchResult = PHAsset.fetchAssets(with: .image, options: options)
photos = fetchResult?.objects(at: IndexSet(0..<(fetchResult?.count ?? 0))) ?? []
}
func photoLibraryDidChange(_ changeInstance: PHChange) {
guard let fetchResult = fetchResult,
let changes = changeInstance.changeDetails(for: fetchResult) else {
return
}
DispatchQueue.main.async {
self.fetchResult = changes.fetchResultAfterChanges
self.photos = changes.fetchResultAfterChanges.objects(at:
IndexSet(0..<changes.fetchResultAfterChanges.count)
)
}
}
}// Over-requesting - picker doesn't need this!
let status = await PHPhotoLibrary.requestAuthorization(for: .readWrite)
if status == .authorized {
showPhotoPicker()
}// Just show the picker - no permission needed
PhotosPicker(selection: $item, matching: .images) {
Text("Select Photo")
}let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
if status == .authorized {
showGallery()
} else {
showPermissionDenied() // Wrong! .limited is valid
}let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
switch status {
case .authorized:
showGallery()
case .limited:
showGallery() // Works with limited selection
showLimitedBanner() // Explain to user
case .denied, .restricted:
showPermissionDenied()
case .notDetermined:
requestAccess()
@unknown default:
break
}// Blocks UI thread
let data = try! selectedItem.loadTransferable(type: Data.self)Task {
if let data = try? await selectedItem.loadTransferable(type: Data.self) {
// Use data
}
}let picker = UIImagePickerController()
picker.sourceType = .photoLibrary
present(picker, animated: true)var config = PHPickerConfiguration()
config.filter = .images
let picker = PHPickerViewController(configuration: config)
present(picker, animated: true).limited.limitedpresentLimitedLibraryPicker().limitedpresentLimitedLibraryPicker()