Using the camera and photo library lets users take pictures or pick photos from their device. This makes apps more interactive and personal.
0
0
Camera and photo library in iOS Swift
Introduction
When you want users to take a new photo inside your app, like for a profile picture.
When you want users to select an existing photo from their gallery.
When building a social app that shares photos.
When creating a document scanner app that uses the camera.
When allowing users to customize content with their own images.
Syntax
iOS Swift
import UIKit class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { func openCamera() { guard UIImagePickerController.isSourceTypeAvailable(.camera) else { return } let picker = UIImagePickerController() picker.sourceType = .camera picker.delegate = self present(picker, animated: true) } func openPhotoLibrary() { let picker = UIImagePickerController() picker.sourceType = .photoLibrary picker.delegate = self present(picker, animated: true) } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { if let image = info[.originalImage] as? UIImage { // use the selected image } picker.dismiss(animated: true) } }
Use UIImagePickerController to access camera or photo library.
Set sourceType to .camera or .photoLibrary depending on what you want.
Examples
This opens the camera for the user to take a new photo.
iOS Swift
let picker = UIImagePickerController() picker.sourceType = .camera present(picker, animated: true)
This opens the photo library so the user can pick an existing photo.
iOS Swift
let picker = UIImagePickerController() picker.sourceType = .photoLibrary present(picker, animated: true)
Sample App
This app shows two buttons: one to open the camera and one to open the photo library. When the user picks or takes a photo, it appears on the screen.
iOS Swift
import UIKit class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { let imageView = UIImageView() let cameraButton = UIButton(type: .system) let libraryButton = UIButton(type: .system) override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white imageView.frame = CGRect(x: 50, y: 100, width: 300, height: 300) imageView.contentMode = .scaleAspectFit view.addSubview(imageView) cameraButton.frame = CGRect(x: 50, y: 420, width: 140, height: 50) cameraButton.setTitle("Open Camera", for: .normal) cameraButton.addTarget(self, action: #selector(openCamera), for: .touchUpInside) view.addSubview(cameraButton) libraryButton.frame = CGRect(x: 210, y: 420, width: 140, height: 50) libraryButton.setTitle("Open Library", for: .normal) libraryButton.addTarget(self, action: #selector(openPhotoLibrary), for: .touchUpInside) view.addSubview(libraryButton) } @objc func openCamera() { guard UIImagePickerController.isSourceTypeAvailable(.camera) else { return } let picker = UIImagePickerController() picker.sourceType = .camera picker.delegate = self present(picker, animated: true) } @objc func openPhotoLibrary() { let picker = UIImagePickerController() picker.sourceType = .photoLibrary picker.delegate = self present(picker, animated: true) } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { if let image = info[.originalImage] as? UIImage { imageView.image = image } picker.dismiss(animated: true) } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.dismiss(animated: true) } }
OutputSuccess
Important Notes
Always check if the camera is available before opening it to avoid crashes.
Remember to add usage descriptions in your app's Info.plist for camera and photo library access.
Handle user denial of permissions gracefully.
Summary
Use UIImagePickerController to let users take or pick photos.
Set sourceType to .camera or .photoLibrary accordingly.
Implement delegate methods to get the selected or taken image.