如何将 GooglePlaces 照片加载到 UITableViewCell UIImageView

How to load GooglePlaces Photo into UITableViewCell UIImageView

我有 GooglePlace PlaceIDs,我正在尝试弄清楚如何将地点照片加载到 UITableView 中。 Google 提供的示例代码显示了如何加载单个 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

您从 Google 的地点详情 API 中获得一组照片 ID photos[] 对应于特定的 placeID.

photos[] — an array of photo objects, each containing a reference to an image. A Place Details request may return up to ten photos. More information about place photos and how you can use the images in your application can be found in the Place Photos documentation. A photo object is described as:

photo_reference — a string used to identify the photo when you perform a Photo request.

height — the maximum height of the image.

width — the maximum width of the image.

html_attributions[] — contains any required attributions. This field will always be present, but may be empty.

您可以在此处查看文档:https://developers.google.com/places/web-service/details

现在要获取 photoID 对应的照片,请使用 Google 的 Place Photo API.

您可以在此处找到文档:https://developers.google.com/places/web-service/photos

示例:

加载UITableViewCell'sphotoID对应的图片 imageView:

    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")
            }
        })
    }