将giphy图像加载到表观中

发布于 2025-02-06 06:15:17 字数 2787 浏览 4 评论 0原文

大家好,所以我试图从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文