将giphy图像加载到表观中
大家好,所以我试图从GIPHY API加载所有GIF图像,但是我一直在下面遇到错误。
2022-06-09 10:39:12.255202-0700 GIPHY App[41186:1667340] [boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
2022-06-09 10:39:12.356240-0700 GIPHY App[41186:1667113] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__SwiftValue 0x600001ec5020> valueForUndefinedKey:]: this class is not key value coding-compliant for the key url.'
以下是我编写的代码,以尝试使用Alamofire和Alamofireimages将图像加载到表视图中。
import UIKit
import Alamofire
import AlamofireImage
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var giphyImage = [GiphyData]()
@IBOutlet weak var giphyTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
fetchGiphyData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return giphyImage.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! GiphyTableViewCell
let gif: GiphyData
gif = giphyImage[indexPath.row]
AF.request(gif.url).responseImage { response in
if let image = response.value {
cell.giphyImage.image = image
}
if case .success(let image) = response.result {
print("image downloaded: \(image)")
}
}
return cell
}
func fetchGiphyData() {
let request = AF.request("https://api.giphy.com/v1/gifs/trending?api_key=zONZGFIbbvYDBjLEBlgKxLjbsClfmptw&limit=25&rating=pg-13")
request.responseDecodable(of: GiphyResponseData.self) { (response) in
guard let gifs = response.value else { return }
let gifImages = [gifs]
for i in 0..<gifImages.count {
self.giphyImage.append(GiphyData(
url: ((gifImages[i] as AnyObject).value(forKey: "url") as! String)
))
}
self.giphyTableView.reloadData()
}
}
}
当我解析JSON信息时,我希望从Giphydata结构中获取URL,但也许这不正确?我是自己尝试项目的新手,因此任何建议/帮助都很棒!
import UIKit
struct GiphyResponseData: Codable {
var data: [ImageInfo]
}
struct ImageInfo: Codable {
var images: ImageTypes
}
struct ImageTypes: Codable {
var original: GiphyData
}
struct GiphyData: Codable {
var url: String
}
谁能告诉我为什么错误会弹出?我从fetchgiphydata函数中获取所需的URL,但表视图没有加载图像。
Hey everyone so I am trying to load all of the gif images from the GIPHY API but I keep getting the error below.
2022-06-09 10:39:12.255202-0700 GIPHY App[41186:1667340] [boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
2022-06-09 10:39:12.356240-0700 GIPHY App[41186:1667113] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__SwiftValue 0x600001ec5020> valueForUndefinedKey:]: this class is not key value coding-compliant for the key url.'
Below is my code that I have written to try and load the images into the table view using alamofire and alamofireimages.
import UIKit
import Alamofire
import AlamofireImage
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var giphyImage = [GiphyData]()
@IBOutlet weak var giphyTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
fetchGiphyData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return giphyImage.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! GiphyTableViewCell
let gif: GiphyData
gif = giphyImage[indexPath.row]
AF.request(gif.url).responseImage { response in
if let image = response.value {
cell.giphyImage.image = image
}
if case .success(let image) = response.result {
print("image downloaded: \(image)")
}
}
return cell
}
func fetchGiphyData() {
let request = AF.request("https://api.giphy.com/v1/gifs/trending?api_key=zONZGFIbbvYDBjLEBlgKxLjbsClfmptw&limit=25&rating=pg-13")
request.responseDecodable(of: GiphyResponseData.self) { (response) in
guard let gifs = response.value else { return }
let gifImages = [gifs]
for i in 0..<gifImages.count {
self.giphyImage.append(GiphyData(
url: ((gifImages[i] as AnyObject).value(forKey: "url") as! String)
))
}
self.giphyTableView.reloadData()
}
}
}
I am looking to grab the urls from the GiphyData struct when I am parsing the JSON info but maybe thats incorrect? I am new to attempting projects on my own so any advice/ help is awesome!
import UIKit
struct GiphyResponseData: Codable {
var data: [ImageInfo]
}
struct ImageInfo: Codable {
var images: ImageTypes
}
struct ImageTypes: Codable {
var original: GiphyData
}
struct GiphyData: Codable {
var url: String
}
Can anyone tell me why the error keeps popping up? I am getting the urls that i need from the fetchGiphyData function but the table view isnt loading the images.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论