前提・実現したいこと
カメラで撮った写真をボタンのsetimageに反映させたい
発生している問題・エラーメッセージ
撮った写真をphotoImageの中に入れて、ボタンに撮った写真を反映させるためにsetImageの中にphotoImageを入れて反映させようとしたのですが全く表示されません。何方か教えてくれると助かります。
該当のソースコード
Swift
1ソースコード 2 @IBOutlet weak var hospitalText: UITextField! 3 4 @IBOutlet weak var examinationText: UITextField! 5 6 @IBOutlet weak var cameraImage: UIButton! 7 8 9 @IBOutlet weak var LoadIndicator: UIActivityIndicatorView! 10 11 12 var photoImage : UIImage! 13 14 var imageFile :String! 15 16 var downloadUrl: String! 17 18 private let db = Firestore.firestore() 19 20 private let userID = Auth.auth().currentUser?.uid 21 22 23 24 25 26 27 override func viewDidLoad() { 28 super.viewDidLoad() 29 LoadIndicator.center = view.center 30 LoadIndicator.style = .large 31 LoadIndicator.color = .blue 32 view.addSubview(LoadIndicator) 33 examinationText.delegate = self 34 hospitalText.delegate = self 35 } 36 37 func textFieldShouldReturn(_ textField:UITextField ) -> Bool{ 38 39 hospitalText.resignFirstResponder() 40 examinationText.resignFirstResponder() 41 return true 42 } 43 44 45 46 func getTime() -> String{ 47 //時間を取得する 48 let dt = Date() 49 let dateFormatter = DateFormatter() 50 //日付の書式&日本時間にする 51 dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "yMMMdHm", options: 0, locale: Locale(identifier: "ja_JP")) 52 let date = dateFormatter.string(from:dt) 53 54 return date 55 } 56 57 func uploadImage() -> String { 58 59 60 let storage = Storage.storage().reference(forURL: String(describing: "Medicine")) 61 62 let imageRef = storage.child("MedicineImage").child("(userID)") 63 64 let imageData:Data = photoImage.pngData()! 65 66 imageRef.putData(imageData,metadata: nil){ (metaData,error) in 67 68 if error != nil{ 69 print(error.debugDescription) 70 return 71 } 72 } 73 imageRef.downloadURL(){ url,err in 74 75 if let url = url { 76 self.downloadUrl = url.absoluteString 77 print("(String(describing: self.downloadUrl))") 78 }else{ 79 print(err as Any) 80 } 81 } 82 return downloadUrl 83 } 84 85 86 87 88 89 90 91 private func Add() { 92 93 imageFile = uploadImage() 94 95 db.collection("users").document("(userID!)").collection("medicinedata").document((getTime())).setData([ 96 "hospitalData": hospitalText.text! , 97 "examinationData": examinationText.text!, 98 "time": getTime(), 99 "photoData": imageFile! 100 ]) 101 } 102 103 104 105 @IBAction func startUiImagePickerController(_ sender: Any) { 106 //カメラを起動する 107 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){ 108 let picker = UIImagePickerController() 109 picker.sourceType = .camera 110 picker.modalPresentationStyle = UIModalPresentationStyle.fullScreen 111 picker.delegate = self 112 present(picker, animated: true, completion: nil) 113 114 } 115 } 116 117 118 private func imagePickController(_ imagePick: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 119 120 if let photo = info[.originalImage] as? UIImage{ 121 photoImage = photo 122 123 cameraImage.setImage(photoImage, for: .normal) 124 imagePick.dismiss(animated: true, completion: nil) 125 126 }else{ 127 128 let dialog = UIAlertController(title: "画像を取得出来ませんでした", message: nil, preferredStyle: .alert) 129 let dialogAction = UIAlertAction(title: "OK", style: .default, handler: nil) 130 dialog.addAction(dialogAction) 131 present(dialog, animated: true, completion: nil) 132 133 } 134 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。