我正在尝试使用Google Places API获取位置照片,使用SDWebImage异步加载图像。但是,图像没有加载。我已经仔细检查了photo_引用是否不正确,但这与我正在提取的JSON数据是相同的。下面是我的代码,显示了我是如何提出请求的。我已经解析了JSON数据,并获得了与每个地方图像相关联的photo_引用。然后我将photo_引用放入一个URL字符串中,该字符串放入一个数组中,并在cellForRowAt表视图方法中调用。
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!)
}
}
}
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/