ios - Google Places API 图片请求网址不返回图片

标签 ios swift google-places-api

我正在尝试使用 Google Places API 使用 SDWebImage 异步加载图像来获取地点照片。但是,图像未加载。我仔细检查了我的 photo_reference 是否不正确,但它与我提取的 JSON 数据相同。下面是我的代码,显示了我是如何发出请求的。我已经解析了 JSON 数据并获得了与每个地点图像关联的 photo_reference。然后我将该 photo_reference 放入一个 URL 字符串中,该字符串被放入一个数组中并在 cellForRowAt TableView 方法中调用。

if let results = myReadableJSON["results"] as? [JSONStandard]{
   for i in 0..<results.count{

       let item = results[i]
       let names = item["name"] as! String

       placeNames.append(names)
       print("The number of names is: ", placeNames.count)
       print("The name(s) is/are: ", placeNames[i],"\n")

      // GETTING PLACE PHOTOS AND PUTTING THEM INTO imageURL ARRAY

       if let photos = item["photos"] as? [JSONStandard]{
            for j in 0..<photos.count{

            let photo = photos[j] as JSONStandard

             let photoRef = photo["photo_reference"] as! String

             let photoURL = NSString(format: "https://maps.googleapis.com/maps/api/place/photo?maxwidth=34&photoreference=%@&key=AIzaSyD3f-rVhs_dNPnsNRorgMDw-MH23k4WrPw", photoRef) as? String

             imageURL.append(photoURL!)

      }
   }
}

TableView方法与SDWebImage方法

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! exploreListTableViewCell

    cell.venuesLabel.text = placeNames[indexPath.row]
    cell.venuesImage.sd_setImage(with: URL(string: imageURL[indexPath.row]), placeholderImage: #imageLiteral(resourceName: " card1"))
    cell.venuesImage.layer.cornerRadius = cell.venuesImage.frame.size.width / 2
    cell.venuesImage.clipsToBounds = true


    return cell
}

最佳答案

根据 google 文档,成功的 Place Photo 请求的响应将是一张图片。图片的类型取决于最初提交的照片的类型。

如果您的请求超出您的可用配额,服务器将返回 HTTP 403 状态并显示您添加的有问题的图像以表明已超出配额。

  1. 因此请检查响应是否有任何错误。

  2. 确保您的 API 无法从 Google 控制台访问并且必须有效。

  3. 检查您的照片引用参数是否有效。

关于ios - Google Places API 图片请求网址不返回图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43811011/

相关文章:

swift - 枚举行为理解(Swift 应用开发介绍 - 第 19 课)

google-places-api - 无法使用 Xcode 7/iOS9 使用 Google Maps API 在路径错误时加载优化模型

android - 查询给定区域中包含全景图的商业场所的api?

iphone - Xcode iOS 理解堆栈跟踪

ios - iOS 应用程序中 UIToolbar 的奇怪行为

uiimageview - 使用 swift 移动和动画 UIImageView

ios - 在 viewDidLoad 中调用扩展

android - 从 .Net MVC 应用程序连接到 Google Places API

ios - 将投影矩阵添加到opengl es点 Sprite 粒子效果顶点着色器

ios - 通过函数使用Gesture时类型 'any View'不能符合 'View'