PhotoKit

RSS for tag

Work with image and video assets managed by the Photos app, including those from iCloud Photos and Live Photos, using PhotoKit.

Posts under PhotoKit tag

87 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Sort user library assets by date captured instead of recently added
Is it possible to sort the user library assets by date captured? The Photos app in iOS 18 lets you choose between Date Captured and Recently Added and I want to offer that same choice in my app. This seems to always sort them by creation date (which I believe is the same as recently added): let assetCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil).firstObject! let fetchResult = PHAsset.fetchAssets(in: assetCollection, options: PHFetchOptions.imageMediaType())
0
1
204
Jun ’24
Is it required to update photo metadata to set 1 for Orientation?
I've had an app that edits photos in your library since the PhotoKit API was released in iOS 8. I know it was required if you preserve photo metadata you had to change the value of Orientation to 1 (up), otherwise PhotoKit would fail to perform the asset change request. When I remove this code, I'm seeing Orientation is getting changed to 1 automatically both at root and in the TIFF dictionary (tested with iOS 18). I wanted to confirm this is expected behavior, the system does this for us now? If so, can I remove this code for iOS 15+, or was it a recent iOS version this started happening? Thanks!
0
0
198
Jun ’24
How to migrate PHPhotoLibraryChangeObserver to Swift 6?
I have the following code: extension AssetGridViewController: PHPhotoLibraryChangeObserver { func photoLibraryDidChange(_ changeInstance: PHChange) { Task { @MainActor in guard let changes = changeInstance.changeDetails(for: fetchResult) else { return } fetchResult = changes.fetchResultAfterChanges } } } With Swift 6, this generates a compilation error: Main actor-isolated instance method 'photoLibraryDidChange' cannot be used to satisfy nonisolated protocol requirement. The error includes to fix-it suggestions: Adding nonisolated to the function (nonisolated func photoLibraryDidChange(_ changeInstance: PHChange)) Adding @preconcurrency to the protocol conformance (extension AssetGridViewController: @preconcurrency PHPhotoLibraryChangeObserver {) Both options generate a runtime error: EXC_BREAKPOINT (code=1, subcode=0x105b7c400). For context, AssetGridViewController is a regular UIViewController. Any ideas on how to fix this?
2
0
242
2w
PHPickerViewController in Limited Access photos mode
Dear Experts, In "limited access" photos mode, I present a PHPickerViewController. It shows the entire photo library, with a note at the top saying that the app can only access the items that I select. I select a photo. In the delegate method, I get a PHPickerResult containing a plausible-looking string for the assetIdentifier. It's the same string that I get for that photo in "full access" mode. Should this photo now be accessible, or do I need to do something else at this point? When I call fetchAssetsWithLocalIdentifiers using this assetIdentifier, I get no results. When I call cloudIdentifierMappingsForLocalIdentifiers, I get error PHPhotosErrorIdentifierNotFound. In "full access" mode, both work OK. What am I missing? Thanks.
3
0
247
1w
Photos Picker Selection
Hi! I am having a bit of trouble with the Photos Picker. In my app, users are able to select photos to appear in a grid, right in the app. I am using the new Photos Picker with SwiftUI. I want to be able to have my users select the images after they have been added to the View. So I want there to be a select button in the top toolbar on the leading side, and then once the user hits the select button, they can select the photos they want to remove on the grid, just like in the photos app, and then where the button to add photos originally is, there will be a trash icon to remove the selected photos from the grid. How would I do this? I have attached my code below for my view, as well as my PhotoPicker: import PhotosUI struct LifestyleImagePicker: View { @StateObject var imagePicker = ImagePicker() @State private var showingDetail = false @State private var selectedIndex = 0 @State private var isSelecting = false @State private var isAddingPhoto = false let columns = [GridItem(.adaptive(minimum: 100))] var body: some View { NavigationSplitView { VStack { if !imagePicker.images.isEmpty { ScrollView { LazyVGrid(columns: columns, spacing: 3) { ForEach(imagePicker.images.indices, id: \.self) { index in imagePicker.images[index] .resizable() .scaledToFit() .onTapGesture { selectedIndex = index showingDetail = true } } } } } else { Text("Tap the plus icon to add photos to your own Inspo Board.") .multilineTextAlignment(.center) } } .padding() .navigationTitle("Lifestyle") .toolbar { ToolbarItem(placement: .navigationBarTrailing) { PhotosPicker(selection: $imagePicker.imageSelections, maxSelectionCount: 10, matching: .images, photoLibrary: .shared()) { Image(systemName: "photo.badge.plus") .imageScale(.large) } } } } detail: { Text("Pick your lifestyle") } .sheet(isPresented: $showingDetail) { DetailImageView(images: $imagePicker.images, selectedIndex: selectedIndex) } } } #Preview { LifestyleImagePicker() } import PhotosUI import Combine import Foundation @MainActor class ImagePicker: ObservableObject { @Published var image: Image? @Published var images: [Image] = [] @Published var imageSelection: PhotosPickerItem? { didSet { if let imageSelection { Task { try await loadTransferable(from: imageSelection) } } } } @Published var imageSelections: [PhotosPickerItem] = [] { didSet { Task { if !imageSelections.isEmpty { try await loadTransferable(from: imageSelections) imageSelections = [] } } } } func loadTransferable(from imageSelections: [PhotosPickerItem]) async throws { do { for imageSelection in imageSelections { if let data = try await imageSelection.loadTransferable(type: Data.self) { if let uiImage = UIImage(data: data) { self.images.append(Image(uiImage: uiImage)) } } } } catch { print(error.localizedDescription) } } func loadTransferable(from imageSelection: PhotosPickerItem?) async throws { do { if let data = try await imageSelection?.loadTransferable(type: Data.self) { if let uiImage = UIImage(data: data) { self.image = Image(uiImage: uiImage) } } } catch { print(error.localizedDescription) image = nil } } }
1
0
89
2d