首先在 UploadImageView 繼承中加入 UIImagePickerController
class UploadImageView: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var imagePicker: UIImagePickerController!
...
override func viewDidLoad() {
super.viewDidLoad()
image.delegate = self
imagePicker
}
}
接著,把上傳動作指定給 Button
present(imagePickerBtn, animated: true, completion: nil)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
//將上傳的圖片轉成 UIImage
if let image = info[UIImagePickerControllerOriginalImage] as? [UIImage] {
iv_userImage.image = image
}
imagePicker.dismiss(animated: true, completion: nil)
}