Challenge - 5 Problems
Camera and Photo Library Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when UIImagePickerController sourceType is set to .camera?
Consider this Swift code snippet in an iOS app:
What will the user see after this code runs?
let picker = UIImagePickerController() picker.sourceType = .camera present(picker, animated: true)
What will the user see after this code runs?
iOS Swift
let picker = UIImagePickerController() picker.sourceType = .camera present(picker, animated: true)
Attempts:
2 left
💡 Hint
Think about what .camera means in UIImagePickerController.
✗ Incorrect
Setting sourceType to .camera tells the app to open the device's camera interface so the user can take a new photo.
🧠 Conceptual
intermediate2:00remaining
Why must you check UIImagePickerController.isSourceTypeAvailable before using .camera?
In iOS development, before setting UIImagePickerController's sourceType to .camera, why should you check if the camera is available using isSourceTypeAvailable(.camera)?
Attempts:
2 left
💡 Hint
Think about devices like iPads without cameras or simulators.
✗ Incorrect
Not all devices have a camera. Checking availability prevents the app from crashing when trying to access a missing camera.
❓ lifecycle
advanced2:00remaining
What delegate method is called after a user picks a photo from the library?
When using UIImagePickerController to let the user pick a photo from the photo library, which delegate method is called after the user selects a photo?
Attempts:
2 left
💡 Hint
This method provides the selected image info.
✗ Incorrect
The delegate method imagePickerController(_:didFinishPickingMediaWithInfo:) is called when the user finishes picking a photo.
🔧 Debug
advanced2:00remaining
Why does this code crash when accessing the photo library?
Given this Swift code snippet:
The app crashes on devices running iOS 14 or later. What is the most likely cause?
let picker = UIImagePickerController() picker.sourceType = .photoLibrary present(picker, animated: true)
The app crashes on devices running iOS 14 or later. What is the most likely cause?
iOS Swift
let picker = UIImagePickerController() picker.sourceType = .photoLibrary present(picker, animated: true)
Attempts:
2 left
💡 Hint
Check app permissions in Info.plist.
✗ Incorrect
iOS requires a usage description key in Info.plist to allow photo library access. Missing it causes a crash.
expert
2:00remaining
What is the correct way to dismiss UIImagePickerController after photo selection?
After the user picks a photo using UIImagePickerController, which code snippet correctly dismisses the picker and processes the image?
iOS Swift
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// process image
}Attempts:
2 left
💡 Hint
Dismiss the picker itself, not the whole view controller.
✗ Incorrect
You must call dismiss on the picker to close it, optionally handling image processing in the completion block.