iOS加载gif动态背景

官方没有直接加载gif动态图的控件,目前将gif文件转为Data类型,通过
WKWebView加载Data类型数据,具体代码如下:

       let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        webConfiguration.allowsAirPlayForMediaPlayback = true
        let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: view.frame.size.height), configuration: webConfiguration)
        self.view.addSubview(webView)
        let filePath = Bundle.main.path(forResource: "4", ofType: "gif")
        let gif = NSData(contentsOfFile: filePath!)! as Data
        webView.load(gif, mimeType: "image/gif", characterEncodingName: "", baseURL: URL.init(fileURLWithPath: filePath!))

你可能感兴趣的:(iOS加载gif动态背景)