如何从 Google 本书 Api 中获取更高分辨率的图像?

How to get higher res image from Google Books Api?

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

这是我的 url:

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
}

这就是我从 url

下载图像的方式
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)
    }
}

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

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

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

URL 的这一部分是您可以根据需要更改图像大小的地方

?fife=w400-h600