신속하게 선택할 때 사진 라이브러리가 표시되지 않는 이유는 무엇입니까?

Dec 09 2020

이 질문을 사용 하여 사용자에게 카메라를 사용할지 아니면 휴대폰에서 이미지를 선택할지 결정하도록 요청합니다.

//Show alert to select the media source type.
    private func showAlert() {

        let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
            self.imagePicker.sourceType = .camera
        }))
        alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
            self.imagePicker.sourceType = .photoLibrary
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
            self.present(alert, animated: true, completion: nil)
    }

나는 다음 viewDidLoad과 같이 요구합니다.

override func viewDidLoad() {
        super.viewDidLoad()
        firstTextField.delegate = self
        showAlert()
        present(imagePicker, animated: true, completion: nil)
        imagePicker.delegate = self
        firstImageView.layer.cornerRadius = 8
        
    }

그러나 이것을 테스트하면 경고가 나타나고 사진 라이브러리를 선택했지만 라이브러리가 나타나지 않습니다. 나는 사용해 viewDidAppear보았지만 작동하지 못했습니다. 오류가 나타나지 않고 경고를 숨기고 현재 뷰 컨트롤러를 표시합니다.

답변

1 matt Dec 09 2020 at 00:00

경고가 표시되면 코드가 중지되는 것을 상상할 수 있습니다. 하지만 그렇지 않습니다! 그리고 두 개의 제시된 뷰 컨트롤러를 동시에 표시 할 수 없습니다. 그러나 그것이 당신이하려는 것입니다.

라고 말하고 showAlert()있으므로 이제 경고가 설정되고 즉시라고 말하는 present(imagePicker)것입니다. 경고가 아직 진행 중이므로 수행 할 수 없습니다.

showAlert경고 의 작업 처리기를 사용하여 이미지 선택기를 표시합니다. 이렇게하면 경고가 닫히고 이미지 선택기가 열립니다.