###前提・実現したいこと
地図的なアプリを作っているのですが、ピンの画像を、FirebaseStorageに格納している個別の画像に変更したいです。
コードの前半部分は関係ないと思いますが、初心者でどこまでお見せすればよいかわからないため全体を公開します。
###発生している問題・エラーメッセージ
ピンの画像がデフォルトのまま変わりません。
画像の取得自体はうまくいっているようで、print(imageimage!)では格納している画像についての情報を読めるのですが、実際のピンはデフォルトのままです。
また、ビルドエラーで一番下の}で
Missing return in a function expected to return 'MKAnnotationView!'
とエラーメッセージが出てきます。
returnはしているつもりなのですが、どこか間違っているのでしょうか?
###該当のソースコード
swift3
1 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 2 if annotation === mapView.userLocation { // 現在地を示すアノテーションの場合はデフォルトのまま 3 return nil 4 } else { 5 //配列の該当のデータをitemという定数に代入? 6 let items = contentArray 7 //画像を取ってくる、まずはFirebaseStorageに接続 8 let storage = Storage.storage() 9 10 // Create a storage reference from our storage service 11 let storageRef = storage.reference(forURL: "https://firebasestorage.googleapis.com/v0/b/catmap-44f0a.appspot.com/o/") 12 13 for item in items { 14 //itemの中身を辞書型に変換? 15 let content = item.value as! Dictionary <String, Any> 16 for (key, element) in content{ 17 let pinData = content[key] as! [String: Any] 18 if let geohash = pinData["geohash"] as? String { 19 20 let (latitude, longitude) = Geohash.decode(hash: geohash)! 21 let annotation = MKPointAnnotation() 22 annotation.coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(latitude.min), CLLocationDegrees(longitude.min)) 23 annotationArray.append(annotation) 24 25 let reuseId = "pin" 26 if let pinView = self.mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) { 27 return pinView 28 } 29 else { // 再利用できるアノテーションが無い場合(初回など)は生成する 30 let pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 31 32 // Firebase Storageの該当imageのReference 33 let catRef = storageRef.child("images/\(key)/0.jpg") 34 35 // Fetch the download URL 36 catRef.downloadURL { (URL, error) -> Void in 37 if (error != nil) { 38 // Handle any errors 39 } else { 40 // Get the download URL for 'images/stars.jpg' 41 // URLを指定したUIImageの生成例 42 let PictureURL = URL! 43 44 /* 45 デフォルト設定でセッションオブジェクトを作成する。 46 */ 47 let session = URLSession(configuration: .default) 48 /* 49 ダウンロードタスクを定義します。ダウンロードタスクは、 50 URLの内容をデータオブジェクトとしてダウンロードし、 51 そのデータで望むことを実行できます。 52 */ 53 let downloadPicTask = session.dataTask(with: catPictureURL) { (data, response, error) in 54 /* 55 ダウンロードが完了しました。 56 */ 57 58 if let e = error { 59 print("cat pictureのダウンロード中にエラーが発生しました: \(e)") 60 } else { 61 /* 62 エラーは見つかりませんでした。 63 レスポンスがないと変わってしまいますので、それもチェックしてください。 64 */ 65 if let res = response as? HTTPURLResponse { 66 print("\(res.statusCode)") 67 if let imageData = data { 68 /* 69 最後に、そのデータをイメージに変換し、 70 それを使って望むことをします。 71 */ 72 73 let imageimage = UIImage(data: imageData) 74 print(imageimage!) 75 pinView.image = imageimage 76 pinView.annotation = annotation 77 78 } else { 79 print("画像を取得できませんでした:画像はありません") 80 } 81 } else { 82 print("何らかの理由で応答コードを取得できませんでした") 83 } 84 } 85 } 86 downloadPicTask.resume() 87 } 88 } 89 return pinView 90 } 91 self.mapView.addAnnotations(annotationArray) 92 } 93 } 94 } 95 } 96 }
###補足情報(言語/FW/ツール等のバージョンなど)
swift3
xcode8.3.3

あなたの回答
tips
プレビュー