Swift: Firebase Chat App Part 3 - Taking or Choosing Profile Picture (Real-time) - Xcode 11 - 2020
extension SignUpViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
private func presentPhotoActionSheet() {
let actionSheet = UIAlertController(title: "Profile Pciture", message: "How will you like to select a picture?", preferredStyle: .actionSheet)
...
}
private func presentCamera() {
...
}
private func presentPhotoPicker() {
...
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
...
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
UIImagePickerController
접근 private func presentCamera() {
let vc = UIImagePickerController()
vc.sourceType = .camera
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
private func presentPhotoPicker() {
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
UIImagePickerController
의 소스 타입 중 카메라, 앨범 선택 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else { return }
DispatchQueue.main.async {
self.imageView.image = selectedImage
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
private func presentPhotoActionSheet() {
let actionSheet = UIAlertController(title: "Profile Pciture", message: "How will you like to select a picture?", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { [weak self] _ in
guard let self = self else { return }
self.presentCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Choose Photo", style: .default, handler: { [weak self] _ in
guard let self = self else { return }
self.presentPhotoPicker()
}))
present(actionSheet, animated: true)
}