2017-10-10 69 views
0

我擁有GooglePlace PlaceID,並且試圖找出如何將照片加載到UITableView中。 ,谷歌提供了顯示瞭如何將示例代碼加載一個單一的UIImageView,並能正常工作:如何將GooglePlaces照片加載到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

你得到photos[]對應於特定placeID谷歌的地方詳細 API照片ID數組。

photos [] - 一組照片對象,每個照片對象都包含對圖像的引用 。地點詳情請求可能會返回最多10張照片。更多 有關地點照片的信息以及如何使用 應用程序中的圖像可以在地點照片文檔中找到。照片 對象被描述爲:

photo_reference - 用於在執行照片請求時識別照片的字符串。

高度 - 圖像的最大高度。

寬度 - 圖像的最大寬度。

html_attributions [] - 包含任何所需的歸因。該字段將始終存在,但可能爲空。

你可以看一下這裏的文檔:https://developers.google.com/places/web-service/details

我們得到對應於photoID照片,使用谷歌的地方照片 API。

您可以在這裏找到的文檔:https://developers.google.com/places/web-service/photos

例子:

要加載對應於photoID圖像中UITableViewCell'simageView

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

非常感謝您的幫助。這很棒! –

相關問題