๋ฐ์ํ
- UIImagePickerController ๊ฐ์ฒด ์์ฑ
private lazy var picker = UIImagePickerController().then {
$0.delegate = self
}
or
var picker = UIImagePickerController()
override func viewDidLoad() {
picker.delegate = self
}
- ๋ฒํผ target ์ค์
imageButton.addTarget(self, action: #selector(presentPhotoPicker), for: .touchUpInside)
@objc func presentPhotoPicker() {
present(picker, animated: true)
}
- UIImagePickerControllerDelgate & UINavigationControllerDelegate ๊ตฌํ
extension DetailViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
messageTextView.image = image
picker.dismiss(animated: true)
}
}
}
์ด๋ฏธ์ง ์ ํ ํ didFinishPickingMediaWithInfo๊ฐ ํ๋ฒ ํธ์ถ ๋๋ ๊ฒ์ด ์๋๋ผ ์ด๋ฏธ์ง๋ฅผ ํญํ ๋งํผ ํธ์ถ ๋จ
๊ฐค๋ฌ๋ฆฌ์์ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ๋น ๋ฅด๊ฒ ํญํ๋ฉด ์ฌ๋ฌ๋ฒ ํธ์ถ๋๋ฉด์ ์ฝ๋๊ฐ ๋ฐ๋ณต๋๋๋ฐ, ์ด๋ dismiss๊ฐ ์ฌ๋ฌ๋ฒ ํธ์ถ๋๋ฉด picker๋ฅผ presentํ vc๋ dismiss ๋ ์ ์์ผ๋ฏ๋ก picker.dismiss(animated:)๋ก ํด์ฃผ์ด์ผ ํจ
๋ง์ฝ image๋ฅผ ๋ฐ์์ ์ด๋ ์ด์ append ์์ผ์ฃผ์ด์ผ ํ๋ ๊ฒฝ์ฐ ๋น ๋ฅด๊ฒ ํญํ ์ด๋ฏธ์ง๊ฐ ์ฌ๋ฌ๋ฒ append ๋ ์ ์์ผ๋ฏ๋ก ํ๋๊ทธ ๋ณ์ ์์ฑํด์ ๋ง์ ์ค ์ ์์
private var isEnableAddPhoto = true
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if isEnableAddPhoto {
isEnableAddPhoto = false
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
picker.dismiss(animated: true) {
self.viewModel?.dataSource.append(image)
self.collectionView.reloadData()
self.isEnableAddPhoto = true
}
}
}
}
rx ์ฐ๋ฉด ์ค๋ณต์ผ๋ก append ๋๋๊ฑฐ ๋ง์ ์ ์์ ํ ๋ฐ...๐ใ ใ
728x90
๋ฐ์ํ
๋๊ธ