【问题标题】:Load gif on UIImage by KingfisherKingfisher 在 UIImage 上加载 gif
【发布时间】:2017-11-01 23:52:20
【问题描述】:

我想在单元格中使用 JSQMessageView 在 UIImage 上加载 gif 图像。我无法访问任何 UIImageView 所以我这样做是为了改变媒体视图。但是,图像没有动画。有没有可能做我想做的事?

这是我的代码:

let urlString = Helpers.getUrlFromGiphyText(text: message.body!)
if let url = URL(string: urlString) {
  KingfisherManager.shared.retrieveImage(with: url, options: 
  [.processor(DefaultImageProcessor.default), 
  .cacheSerializer(FormatIndicatedCacheSerializer.gif)], 
  progressBlock: 
  { receivedSize, totalSize in
  print("(indexPath.row + 1): (receivedSize)/(totalSize)")
  }, completionHandler: { image, error, cacheType, imageURL in
  if let gifImage = image {
   let mediaItem = JSQPhotoMediaItem(image: gifImage)
   cell.mediaView = mediaItem?.mediaView()
  }
})
}

【问题讨论】:

    标签: ios swift jsqmessagesviewcontroller kingfisher


    【解决方案1】:

    我为我的应用编写了这个媒体提取器方法,它适用于 gif 文件。

    注意:请注意,我在 kingfisher 管理器下载每个文件后使用 DispatchQueue.main.asyncAfter(deadline: .now() + 0.5),因为没有延迟 - 首次下载时不会播放 gif。 我在这件事上搞砸了。我认为这可能是 Kingfisher 的一些缓存同步问题。

    mediaFetcher.downloadGifFile(url) { imageUrl in
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            // Use whatever u want to load the gif file
        }
    }
    
    class MediaFetcher {
        func downloadGifFile(_ url: URL, completion: @escaping (_ imageURL: URL) -> () = { (imageURL) in} ) {
            KingfisherManager.shared.retrieveImage(with: url, options: .none, progressBlock: nil) { (image, error, cacheType, imageUrl) in
                guard error == nil else {
                    return
                }
                guard let imageUrl = imageUrl else {
                    return
                }
                completion(imageUrl)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      相关资源
      最近更新 更多