使用Swift中的AlamofireImage将GIF图像映射到UIImageView

问题描述 投票:1回答:1

我正在使用AlamofireImage库来下载/缓存Web图像并在tableViewCells中的UIImageView中显示它。

imageView.af_setImage(withURL: url, placeholderImage: nil, filter: nil, imageTransition: .crossDissolve(0.3), runImageTransitionIfCached: true, completion: { (response) in
//...... other code .....
})

它适用于.png / .jpg或其他静止图像,但我无法使用此功能显示GIF图像。

我尝试使用外部库将imageData转换为gif图像,但它工作得很完美,但Alamofire并没有缓存gif数据,下次将图像加载为静止图像。

检查以下代码:

imageView.af_setImage(withURL: url, placeholderImage: nil, filter: nil, imageTransition: .crossDissolve(0.3), runImageTransitionIfCached: true, completion: { (response) in

        if imageUrl.hasSuffix("gif") {
            if let data = response.data{
                self.imageView.image = UIImage.gifImageWithData(data)
            }
        }
    })

上面的代码首次显示GIF,但下次只显示静止图像。

知道如何使用AlamofireImage实现以下功能:

  1. 首次下载GIF imageData,将其缓存并向GFS格式显示
  2. 下次从缓存中获取imageData并再次显示GIF
ios swift uiimageview gif alamofireimage
1个回答
-1
投票
  • 下载GIF文件,下次获取文件并显示 Alamofire.download(gifUrl, to: destination).responseJSON(completionHandler: { response in if let filePath = response.destinationURL?.path{ if let gifImage = UIImage.init(contentsOfFile: filePath){ self.imageVIew.image = gifImage } } })
© www.soinside.com 2019 - 2024. All rights reserved.