Skip to content

Instantly share code, notes, and snippets.

@shaundon
Last active July 4, 2022 11:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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)
}
}
}
@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