我正在尝试使用 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 状态并显示您添加的有问题的图像以表明已超出配额。
因此请检查响应是否有任何错误。
确保您的 API 无法从 Google 控制台访问并且必须有效。
检查您的照片引用参数是否有效。
关于ios - Google Places API 图片请求网址不返回图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43811011/