2017-05-05 73 views
0

我试图使用SDWebImage的Google Places API来获取放置照片以异步加载图像。但是,图像未加载。我已经仔细检查过,看看我的photo_reference是否不正确,但这与我正在使用的JSON数据是一样的。以下是我的代码,显示我如何提出请求。我已经通过JSON数据进行了解析并获得了与每个位置图像关联的photo_reference。然后,我把这个photo_reference放到一个URL字符串中,该字符串被放入一个数组中,并在cellForRowAt表视图方法中调用。Google Places API图像请求网址不会返回图像

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 
} 

回答

1

作为每谷歌的文档,一个成功的地方照片请求的响应将是一个图像。图像的类型将取决于最初提交的照片的类型。

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

  1. 因此,检查你的回应是否有任何错误。

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

  3. 检查你的照片ref参数是否有效。

+0

感谢您的评论。我能够通过我遇到的原始问题,但是如果您可以使用一些非常感谢的新代码查看我的更新问题。 – Akhmed

+0

添加你的json数据 –

+0

我能弄清楚我的问题的解决方案。显然,我的API密钥有一个限制,它只能用于iOS。所以,我没有任何限制,现在它抓住了图像。 再次感谢 – Akhmed

相关问题