With WWDC20, Apple added a new framework named PhotosUI to select images, videos etc from Library. In this tutorial, we going to implement the same with SwiftUI.

As PhotosUI is based on UIKit not SwiftUI, We need to write a representable. this representable can use PhotosUI’s “PHPickerViewController” in SwiftUI.

struct PhotoPicker: UIViewControllerRepresentable {

	    func makeUIViewController(context: Context) -> PHPickerViewController {
	      //init PHPickerViewController here
	    }

	    func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { 

	    }

	    func makeCoordinator() -> Coordinator {
	       //set delegates with Coordinator
	    }
	}

Picker

  • _PHPickerViewController _needs a PHPickerConfiguration as initialiser.
  • PhotoPicker will need toggle state to dismiss controller once user is done
  • PhotoPicker will also need a binging object to pass on Images back to caller.
  • Implement PHPickerViewControllerDelegate to handle return Image from Library

#wwdc #programming #swiftui #swift #ios #photosui

SwiftUI: How to use PHPicker(PhotosUI) to select Image from Library?
49.70 GEEK