Sample or Demo SWIFT code using FLAnimatedImage

牧云@^-^@ 提交于 2019-12-07 05:28:36

问题


I'm trying to incorporate FLAnimatedImage in my Swift-based iOS application for the purposes of displaying an animated GIF.

I started my importing the FLAnimatedImage.h and FLAnimatedImageView.m files and created the Bridging-Header.h without issue.

I tried to load the Gif into my ImageView with this code:

@IBOutlet weak var animatedImageView: FLAnimatedImageView! = animatedImageView.animatedImage = FLAnimatedImage(animatedGIFData: NSData(contentsOfFile: "chicken.gif")) `  

But it fails at application startup with the following error code:

exception 'NSInvalidArgumentException', reason: '-[UIImageView setAnimatedImage:]: unrecognized selector sent to instance 0x78e3c820'

Any suggestion on how to fix the above error?

Thanks for any help.


回答1:


import UIKit
class ViewController: UIViewController {
@IBOutlet weak var gifView: FLAnimatedImageView!
override func viewDidLoad() {
  super.viewDidLoad()
  print("\(gifView)")
  if let path =  NSBundle.mainBundle().pathForResource("rock", ofType: "gif") {
    if let data = NSData(contentsOfFile: path) {
      let gif = FLAnimatedImage(animatedGIFData: data)
      gifView.animatedImage = gif
    }
  }
}
}

By the way, did you make your animatedImageView in the storyboard subclassed from FLAnimatedImageView?




回答2:


Swift 4: Gif URL Example

@IBOutlet weak var gifImageView: FLAnimatedImageView!

private func showGifImage(url: URL) {
    DispatchQueue.global().async { [weak self] in
        do {
            let gifData = try Data(contentsOf: url)
            DispatchQueue.main.async {
                self?.gifImageView.animatedImage = FLAnimatedImage(gifData: gifData)
            }
        } catch {
            print(error)
        }
    }
}



回答3:


I got it to work with the following extension:

extension FLAnimatedImage {
    convenience init(gifResource: String) {
        self.init(animatedGIFData: NSData(contentsOfFile:     NSBundle.mainBundle().pathForResource(gifResource, ofType: "")!))
    }
}


Declaration:

@IBOutlet weak var animatedImageView: FLAnimatedImageView!


Call:

animatedImageView.animatedImage = FLAnimatedImage(gifResource: "chicken.gif")

And in the storyboard, the UIImageView has a custom subclass: FLAnimatedImageView



来源:https://stackoverflow.com/questions/33217510/sample-or-demo-swift-code-using-flanimatedimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!