Component - Camera and photo library
This UI component allows users to take a photo using the device camera or select an existing photo from the photo library. It shows the chosen image on the screen.
This UI component allows users to take a photo using the device camera or select an existing photo from the photo library. It shows the chosen image on the screen.
UIViewController ├── UIButton (Take Photo) ├── UIButton (Choose from Library) └── UIImageView
sourceType to .camera in UIImagePickerController?sourceType usagesourceType property of UIImagePickerController defines where the image will come from..camerasourceType to .camera opens the device camera to take a new photo.sourceType = .camera means take new photo [OK]UIImagePickerController and set its delegate in Swift?UIImagePickerController() requires parentheses to create an instance.self without parentheses because it is a property, not a function call.func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
print("Image selected")
} else {
print("No image found")
}
}imagePickerController(_:didFinishPickingMediaWithInfo:) is called after user picks a photo.info dictionary contains the key .originalImage with the selected UIImage if successful.let picker = UIImagePickerController() picker.sourceType = .camera picker.delegate = self present(picker, animated: true)
UIImagePickerController delegate methods, the class must conform to UIImagePickerControllerDelegate and UINavigationControllerDelegate.sourceType flexibilityUIImagePickerController can have its sourceType set to either .camera or .photoLibrary before presenting.sourceType dynamically and present the picker once.Pass the test and this pattern moves unknown