1

当我尝试从谷歌图书 API 获取更高分辨率的图像时,它只会给我 128 X 209 的小缩略图,我无法获得更大的图像

这是我的网址:

if let url = URL(string: "https://www.googleapis.com/books/v1/volumes?q=colleen+hoover")

我的结构:

    struct Book: Identifiable, Codable {
    let id = UUID()
    let volumeInfo: VolumeInfo
}

struct VolumeInfo: Codable {
    let title, publishedDate: String?
    let authors: [String]?
    let publisher, description: String?
    let imageLinks: ImageLinks?
    let averageRating: Double?
    
}

struct ApiResponse: Codable {
    let kind: String
    let totalItems: Int
    let items: [Book]
}

struct ImageLinks: Codable {
    let smallThumbnail, thumbnail: String
}

这就是我从网址下载图像的方式

extension UIImageView {
    func downloaded(from url: URL, contentMode mode: ContentMode = .scaleAspectFit) {
        contentMode = mode
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard
                let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                let data = data, error == nil,
                let image = UIImage(data: data)
            else { return }
            DispatchQueue.main.async() { [weak self] in
                self?.image = image
            }
        }.resume()
    }
    func downloaded(from link: String, contentMode mode: ContentMode = .scaleAspectFit) {
        guard let url = URL(string: link) else { return }
        downloaded(from: url, contentMode: mode)
    }
}
4

1 回答 1

0

好的,我找到了解决此问题的方法,但我不确定这是否是最佳解决方案。

我没有从结构中获取图像 URL 并使用缩略图,而是使用此 URL 并将 bookImgId 变量更改为您的书 ID

https://books.google.com/books/publisher/content/images/frontcover/\(bookImgId)?fife=w400-h600&source=gbs_api

URL 的这一部分是您将图像大小更改为所需大小的地方

?fife=w400-h600
于 2022-01-29T22:31:39.783 回答