2017-05-05 79 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