Swift:使用 UIWebView创建 动态背景图

1 将动态的DIF格式图拖到工程中

2 然后创建一个uiwebView

3 加载GIF图 ,转化为data

 // 获取到 gif 图片
        let filePath = Bundle.main.path(forResource: "BG_mountion", ofType: "gif")
        let url = URL(fileURLWithPath: filePath!)
        var gif = Data()
        do {
            gif  = try Data(contentsOf: url)
        } catch {
            print("data 错误")
        }

        
        // 1.创建webview
        let width = self.view.frame.size.width;
        let height = self.view.frame.size.height;
        let webView = UIWebView(frame: CGRect(x: 0, y: 0, width: width, height: height))
        webView.load(gif, mimeType: "image/gif", textEncodingName: "", baseURL: url)
        self.view.addSubview(webView)

参考文章:
iOS 背景动态图
Swift中Call can throw, but it is not marked with ‘try’ and the error is not handled错误的解决

你可能感兴趣的:(Swift)