비전공자 개발일기

Choose a picture in iPhone's photo library(UIImagePickerController) 본문

SWIFT

Choose a picture in iPhone's photo library(UIImagePickerController)

HiroDaegu 2023. 1. 14. 21:32
728x90
SMALL

시연 영상


위의 기능을 추가하기에 앞서 추가해야할 설정

권한 설정!

 

 

Privacy - Camera Usage Description 카메라 사용 권한
Privacy - Photo Library Additons Usage Description 사진첩 접근 및 사용 권한
extension 해당 화면의 뷰컨트롤러: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func presentPhotoActionSheet() {
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))
        actionSheet.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { [weak self] _ in
            self?.presentCamera()
        }))
        actionSheet.addAction(UIAlertAction(title: "Choose Photo", style: .default, handler: { [weak self] _ in
            self?.presentPhotoPicker()
        }))
        
        present(actionSheet, animated: true)
    }
     
    // 사진첩(Take Photo) 선택 시 보이는 화면 설정
    func presentPhotoPicker() {
        let vc = UIImagePickerController()
        vc.sourceType = .photoLibrary
        vc.delegate = self
        vc.allowsEditing = true
        vc.modalPresentationStyle = .fullScreen
        present(vc, animated: true)
    }
    
    // choose 선택 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true)
//        print(info)
        guard let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else { return }
        self.imageView.image = selectedImage
    }
    
    // cancel 선택
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true)
    }
    
}
728x90
LIST

'SWIFT' 카테고리의 다른 글

Log Out  (0) 2023.01.16
Firebase Auth(Email / Password Login)  (0) 2023.01.15
MVC Pattern  (0) 2023.01.13
UIView Border by storyboard  (0) 2023.01.10
Dark Mode & Light Mode, Current App version check  (0) 2023.01.09