본문 바로가기
iOS

[iOS] [Swift] 카메라 및 앨범 사진 가져오기

by giop15 2022. 2. 28.
반응형

iPhone에서 카메라 및 앨범에서 사진 가져오기에 대해서 알아볼 예정입니다.

 

  • 먼저 info에 카메라 및 앨범 permission 작업을 해줍니다.

 

  • 사진과 앨범을 실행하는데 필요한 UIImagePickerController()를 저장할 변수 선언 및 UIImagePickerControllerDelegate, UINavigationControllerDelegate 프로토콜을 상속받습니다.
class ViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    let imagePicker = UIImagePickerController() // 사진, 앨범을 열 수 있는 이미지 피커 상수

    override func viewDidLoad() {
        super.viewDidLoad()

        self.imagePicker.delegate = self
    }
}

 

  • 카메라 접근 권한을 판별합니다.
/**
 카메라 접근 권한 판별하는 함수
 */
func cameraAuth() {
    AVCaptureDevice.requestAccess(for: .video) { granted in
        if granted {
            print("권한 허용")
            self.openCamera()
        } else {
            print("권한 거부")
            self.showAlertAuth("카메라")
        }
    }
}

 

  • 앨범 접근 권한을 판별합니다.
/**
 앨범 접근 권한 판별하는 함수
 */
func albumAuth() {
    switch PHPhotoLibrary.authorizationStatus() {
    case .denied:
        print("거부")
        self.showAlertAuth("앨범")
    case .authorized:
        print("허용")
        self.openPhotoLibrary()
    case .notDetermined, .restricted:
        print("아직 결정하지 않은 상태")
        PHPhotoLibrary.requestAuthorization { state in
            if state == .authorized {
                self.openPhotoLibrary()
            } else {
                self.dismiss(animated: true, completion: nil)
            }
        }
    default:
        break
    }
}

 

  • 권한 거부 시 설정으로 이동하도록 유도합니다.
/**
 권한을 거부했을 때 띄어주는 Alert 함수
 - Parameters:
 - type: 권한 종류
 */
func showAlertAuth(
    _ type: String
) {
    if let appName = Bundle.main.infoDictionary!["CFBundleDisplayName"] as? String {
        let alertVC = UIAlertController(
            title: "설정",
            message: "\(appName)이(가) \(type) 접근 허용되어 있지 않습니다. 설정화면으로 가시겠습니까?",
            preferredStyle: .alert
        )
        let cancelAction = UIAlertAction(
            title: "취소",
            style: .cancel,
            handler: nil
        )
        let confirmAction = UIAlertAction(title: "확인", style: .default) { _ in
            UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
        }
        alertVC.addAction(cancelAction)
        alertVC.addAction(confirmAction)
        self.present(alertVC, animated: true, completion: nil)
    }
}

 

  • 카메라에 접근하도록 설정합니다.
/**
 아이폰에서 카메라에 접근하는 함수
 */
private func openCamera() {
    if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
        self.imagePicker.sourceType = .camera
        self.imagePicker.modalPresentationStyle = .currentContext
        self.present(self.imagePicker, animated: true, completion: nil)
    } else {
        print("카메라에 접근할 수 없습니다.")
    }
}

 

  • 앨범에 접근하도록 설정합니다.
/**
 아이폰에서 앨범에 접근하는 함수
 */
private func openPhotoLibrary() {
    if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) {
        self.imagePicker.sourceType = .photoLibrary
        self.imagePicker.modalPresentationStyle = .currentContext
        self.present(self.imagePicker, animated: true, completion: nil)
    } else {
        print("앨범에 접근할 수 없습니다.")
    }
}

 

  •  선택한 미디어의 정보를 알 수 있게 설정합니다.
/**
 UIImagePickerControllerDelegate에 정의된 메소드 - 선택한 미디어의 정보를 알 수 있음
 */
func imagePickerController(
    _ picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]
) {
    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        print("image_info = \(image)")
    }
    dismiss(animated: true, completion: nil)
}

 

  • 전체 소스 코드
class ViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
	
    let imagePicker = UIImagePickerController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.imagePicker.delegate = self
    }
    
    /**
     카메라 접근 권한 판별하는 함수
     */
    func cameraAuth() {
        AVCaptureDevice.requestAccess(for: .video) { granted in
            if granted {
                print("권한 허용")
                self.openCamera()
            } else {
                print("권한 거부")
                self.showAlertAuth("카메라")
            }
        }
    }
    
    /**
     앨범 접근 권한 판별하는 함수
     */
    func albumAuth() {
    switch PHPhotoLibrary.authorizationStatus() {
        case .denied:
            print("거부")
            self.showAlertAuth("앨범")
        case .authorized:
            print("허용")
            self.openPhotoLibrary()
        case .notDetermined, .restricted:
            print("아직 결정하지 않은 상태")
            PHPhotoLibrary.requestAuthorization { state in
                if state == .authorized {
                    self.openPhotoLibrary()
                } else {
                    self.dismiss(animated: true, completion: nil)
                }
            }
        default:
            break
        }
    }
    
    /**
     권한을 거부했을 때 띄어주는 Alert 함수
     - Parameters:
     - type: 권한 종류
     */
    func showAlertAuth(
        _ type: String
    ) {
        if let appName = Bundle.main.infoDictionary!["CFBundleDisplayName"] as? String {
            let alertVC = UIAlertController(
                title: "설정",
                message: "\(appName)이(가) \(type) 접근 허용되어 있지 않습니다. 설정화면으로 가시겠습니까?",
                preferredStyle: .alert
            )
            let cancelAction = UIAlertAction(
                title: "취소",
                style: .cancel,
                handler: nil
            )
            let confirmAction = UIAlertAction(title: "확인", style: .default) { _ in
                UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
            }
            alertVC.addAction(cancelAction)
            alertVC.addAction(confirmAction)
            self.present(alertVC, animated: true, completion: nil)
        }
    }
    
    /**
     아이폰에서 앨범에 접근하는 함수
     */
    private func openPhotoLibrary() {
        if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) {
            self.imagePicker.sourceType = .photoLibrary
            self.imagePicker.modalPresentationStyle = .currentContext
            self.present(self.imagePicker, animated: true, completion: nil)
        } else {
            print("앨범에 접근할 수 없습니다.")
        }
    }

    /**
     아이폰에서 카메라에 접근하는 함수
     */
    private func openCamera() {
        if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
            self.imagePicker.sourceType = .camera
            self.imagePicker.modalPresentationStyle = .currentContext
            self.present(self.imagePicker, animated: true, completion: nil)
        } else {
            print("카메라에 접근할 수 없습니다.")
        }
    }

    /**
     UIImagePickerControllerDelegate에 정의된 메소드 - 선택한 미디어의 정보를 알 수 있음
     */
    func imagePickerController(
        _ picker: UIImagePickerController,
        didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]
    ) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            print("image_info = \(image)")
        }
        dismiss(animated: true, completion: nil)
    }
}

 

반응형

'iOS' 카테고리의 다른 글

[iOS] WkWebView에서 window.open / window.close 처리  (0) 2022.06.30
[iOS] 그림 그리기  (0) 2022.04.12
[iOS] 음성 녹음  (0) 2022.03.14
[iOS] 전화걸기  (0) 2022.02.28