Complete the code to import the framework needed to access the photo library.
import UIKit import [1]
The PhotosUI framework is used to access the photo library and camera features in iOS apps.
Complete the code to declare a UIImagePickerController variable.
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { var imagePicker = [1]() }
The UIImagePickerController is the class used to present the camera or photo library interface.
Fix the error in the code to present the image picker for the photo library.
func openPhotoLibrary() {
imagePicker.sourceType = [1]
present(imagePicker, animated: true)
}To open the photo library, set sourceType to photoLibrary.
Fill both blanks to check if the camera is available and set the source type accordingly.
if UIImagePickerController.isSourceTypeAvailable([1]) { imagePicker.sourceType = [2] }
First, check if the camera is available, then set the source type to camera.
Fill all three blanks to implement the delegate method that handles the selected image.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let selectedImage = info[[1]] as? [2] {
imageView.image = selectedImage
}
picker.[3](animated: true, completion: nil)
}The delegate method uses InfoKey.originalImage to get the selected image as a UIImage, then dismisses the picker.