ios - 通过 Kingfisher 在 UIImage 上加载 gif

标签 ios swift jsqmessagesviewcontroller kingfisher

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

这是我的代码:

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()
  }
})
}

最佳答案

我为我的应用程序编写了这个媒体 getter 方法,它可以很好地处理 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)
        }
    }
}

关于ios - 通过 Kingfisher 在 UIImage 上加载 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47058306/

相关文章:

ios - SIgnalR iOS 客户端为 connectionId 和协议(protocol)版本返回 null

ios - 在谷歌地图上显示多个位置。 IOS/Objective-C

iphone - tabBarController 不显示

ios - 打开设置为 UILabel 作为属性字符串的文本上的链接。从 json 响应中读取属性字符串

xcode - 设置导航和状态栏文本颜色

swift - uiAlertController 有时不解雇

ios - InputToolbar 发送按钮不起作用

ios - 使用CoreText将丰富的UITextView中的文本对齐段落?

ios - 如何在键盘已经打开的情况下呈现 JSQMessagesViewController?

ios - JSQMessagesViewController 后退按钮不可见