Skip to content

利用Qt实现Gif转预览图片

struct NetworkReply
{
    QNetworkReply::NetworkError error = QNetworkReply::HostNotFoundError;
    QByteArray data;

    QString ErrorStr();
};

void GitHubImageHost::DownloadImage(GHImage &gh_image)
{
    if (gh_image.pix.isNull()) {

        auto reply = NetworkAccess::Request(QUrl(gh_image.download_url));

        if (!gh_image.html_url.right(5).contains("gif")) {
            gh_image.pix = QImage::fromData(reply.data).scaled(img_size_, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        } else {
            QBuffer buffer;
            buffer.open(QFile::WriteOnly);
            buffer.write(reply.data);
            buffer.close();
            QImageReader reader(&buffer);
            gh_image.pix = reader.read().scaled(img_size_, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        }
    }
}