Skip to content

Instantly share code, notes, and snippets.

@shaundon
Last active July 4, 2022 11:31
Show Gist options
  • Save shaundon/56c8a5801c5dc63c2e4a0af9b39c8f7b to your computer and use it in GitHub Desktop.
Save shaundon/56c8a5801c5dc63c2e4a0af9b39c8f7b to your computer and use it in GitHub Desktop.
PHPicker in SwiftUI
import SwiftUI
struct ContentView: View {
@State private var showPhotoSheet = false
@State private var image: UIImage? = nil
var body: some View {
VStack {
Button(action: { showPhotoSheet = true }) {
Label("Choose photo", systemImage: "photo.fill")
}
.fullScreenCover(isPresented: $showPhotoSheet) {
PhotoPicker(filter: .images, limit: 1) { results in
PhotoPicker.convertToUIImageArray(fromResults: results) { (imagesOrNil, errorOrNil) in
if let error = errorOrNil {
print(error)
}
if let images = imagesOrNil {
if let first = images.first {
print(first)
image = first
}
}
}
}
.edgesIgnoringSafeArea(.all)
}
if let image = image {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxWidth: 200, maxHeight: 200)
}
}
}
}
import SwiftUI
import PhotosUI
struct PhotoPicker: UIViewControllerRepresentable {
typealias UIViewControllerType = PHPickerViewController
let filter: PHPickerFilter
var limit: Int = 0 // 0 == 'no limit'.
let onComplete: ([PHPickerResult]) -> Void
func makeUIViewController(context: Context) -> PHPickerViewController {
var configuration = PHPickerConfiguration()
configuration.filter = filter
configuration.selectionLimit = limit
let controller = PHPickerViewController(configuration: configuration)
controller.delegate = context.coordinator
return controller
}
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
parent.onComplete(results)
picker.dismiss(animated: true)
}
private let parent: PhotoPicker
init(_ parent: PhotoPicker) {
self.parent = parent
}
}
static func convertToUIImageArray(fromResults results: [PHPickerResult], onComplete: @escaping ([UIImage]?, Error?) -> Void) {
var images = [UIImage]()
let dispatchGroup = DispatchGroup()
for result in results {
dispatchGroup.enter()
let itemProvider = result.itemProvider
if itemProvider.canLoadObject(ofClass: UIImage.self) {
itemProvider.loadObject(ofClass: UIImage.self) { (imageOrNil, errorOrNil) in
if let error = errorOrNil {
onComplete(nil, error)
dispatchGroup.leave()
}
if let image = imageOrNil as? UIImage {
images.append(image)
dispatchGroup.leave()
}
}
}
}
dispatchGroup.notify(queue: .main) {
onComplete(images, nil)
}
}
}
@skywalkerlw
Copy link

i can see this kind of error

2021-07-21 12:35:34.330489+1000 Test-iOS[5548:107098] [claims] Upload preparation for claim 57009F46-C1D9-4C74-89F4-9C304204B79F completed with error: Error Domain=NSCocoaErrorDomain Code=260 "The file “version=1&uuid=CC95F08C-3222-4012-9D6D-64A413D254B3&mode=compatible.jpeg” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///Users/skyeluke/Library/Developer/CoreSimulator/Devices/BD930028-4DEC-4EF2-84E9-E337FC273062/data/Containers/Shared/AppGroup/23223-302B-4B84-BA5C-AEE9AA17BE8F/File%20Provider%20Storage/photospicker/version=1&uuid=CC95F08C-88C3-4012-9D6D-64A413D254B3&mode=compatible.jpeg, NSFilePath=/Users/skyeluke/Library/Developer/CoreSimulator/Devices/BD930028-4DEC-4EF2-84E9-E337FC273062/data/Containers/Shared/AppGroup/234322-302B-4B84-BA5C-AEE9AA17BE8F/File Provider Storage/photospicker/version=1&uuid=CC95F08C-3322-4012-9D6D-64A413D254B3&mode=compatible.jpeg, NSUnderlyingError=0x6000030c7150 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
PhotoPicker error: Cannot load representation of type public.jpeg

@shaundon
Copy link
Author

I’m no expert but it looks like the photo being chosen isn’t actually there – how does it perform on a real device (not the simulator)?

@az-igor
Copy link

az-igor commented Oct 26, 2021

Looks like a bug in the simulator. I can see exactly the same error in the simulator when I pick the top left standard image (even the UUID is exactly the same as above). Any other image is picked successfully. No such issue seen on the device. Neither it is seen in the simulator when UIImagePickerController is used instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment