2017-10-10 18 views
0

私はGooglePlaceのPlaceIDを持っており、場所の写真をUITableViewに読み込む方法を理解しようとしています。 Googleは、単一のUIImageViewをロードする方法を示していますを提供し、これは正常に動作するサンプルコード:GooglePlaceの写真をUITableViewCellに読み込む方法UIImageView

func loadFirstPhotoForPlace(placeID: String) { 
     GMSPlacesClient.shared().lookUpPhotos(forPlaceID: placeID) { (photos, error) -> Void in 
      if let error = error { 
       // TODO: handle the error. 
       print("Error: \(error.localizedDescription)") 
      } else { 
       if let firstPhoto = photos?.results.first { 
        self.loadImageForMetadata(photoMetadata: firstPhoto) 
       } 
      } 
     } 
    } 

    func loadImageForMetadata(photoMetadata: GMSPlacePhotoMetadata) { 
     GMSPlacesClient.shared().loadPlacePhoto(photoMetadata, callback: { 
      (photo, error) -> Void in 
      if let error = error { 
       // TODO: handle the error. 
       print("Error: \(error.localizedDescription)") 
      } else { 
       print("Loading Image") 
       self.checkInImageView.image = photo; 
     //  self.attributionTextView.attributedText = photoMetadata.attributions; 
      } 
     }) 
    } 

私が直接場所の写真をダウンロードする方法のドキュメントから見つけ出すことはできません。多くの失敗した試みの一つは:

if let placeID = checkins[indexPath.row].placeID { 
     GMSPlacesClient.shared().lookUpPhotos(forPlaceID: placeID) { (photos, error) -> Void in 
     if let firstPhoto = photos?.results.first { 
      cell.thumbnailImageView.image = firstPhoto 
     } 
    } 

    } 
    return cell 

答えて

1

Googleの場所の詳細 APIは、特定のplaceIDに対応photos[]から写真IDの配列を取得します。

photos [] - フォトオブジェクトの配列。それぞれが のイメージへの参照を含んでいます。プレイス詳細リクエストでは、最大10枚の写真を返すことができます。 場所の写真に関する情報と、 アプリケーションでの画像の使用方法については、写真の配置に関するドキュメントをご覧ください。写真 オブジェクトは、次のように記述されています。

photo_reference - フォトリクエストを実行するときに写真を識別するために使用される文字列。

高さ - 画像の最大高さ。

幅 - 画像の最大幅。

html_attributions [] - 必要な属性がすべて含まれています。このフィールドは常に表示されますが、空の場合もあります。

現在のドキュメントを見ることができます:Googleの場所の写真 APIを使用して、photoIDに対応した写真を取得するために、今すぐhttps://developers.google.com/places/web-service/details

あなたがここにドキュメントを見つけることができます。https://developers.google.com/places/web-service/photos

例:

UITableViewCell'simageViewphotoIDに対応する画像をロードするには:

let urlString = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=\(Int(UIScreen.main.bounds.width))&photoreference=\(photoID)&key=API_KEY" 

    if let url = URL(string: urlString) 
    { 
     let urlRequest = URLRequest(url: url) 
     NSURLConnection.sendAsynchronousRequest(urlRequest, queue: OperationQueue.main, completionHandler: {(response, data, error) in 
      if let data = data 
      { 
       let image = UIImage(data: data) 
       cell.photoImageView.image = image 
      } 
      else 
      { 
       cell.photoImageView.image = UIImage(named: "PlaceholderImage") 
      } 
     }) 
    } 
+0

おかげであなたの助けのために非常に多くの。これは素晴らしい! –

関連する問題