UIWebView 中的动图

Gif in UIWebView

var filePath = NSBundle.mainBundle().pathForResource("long1", ofType: "gif")
var gif = NSData(contentsOfFile: filePath!)     
var webViewBG = UIWebView(frame: view.frame)
webViewBG.loadData(gif, MIMEType: "image/gif", textEncodingName: nil, baseURL: nil)
webViewBG.userInteractionEnabled = false
self.view.insertSubview(webViewBG, atIndex: 0)

这是我添加加载了 Gif 的 webview 的代码。但是,虽然网络视图可能会填充 view.frame,但 .gif 不会填充网络视图。因此,gif 周围有灰色边框。

我试过 sizetoscale 和其他各种技巧,但都没有用!

编辑

UIImage+animatedGIF 效果很好

我不确定你试过什么,没试过什么,但是你试过了吗:

webViewBG.stringByEvaluatingJavaScriptFromString("document.body.style.margin='0';document.body.style.padding = '0'")

webview加载完成后?您可以通过在创建 webview 时将自己指定为委托来执行此操作:

webViewBG.delegate = self

然后在你的 class 中添加 webViewDidFinishLoad,像这样:

func webViewDidFinishLoad(webView: UIWebView) {
    webView.stringByEvaluatingJavaScriptFromString("document.body.style.margin='0';document.body.style.padding = '0'")
}

您是否只是因为需要显示 gif 而使用 webview?因为您也可以显示 gif。使用UIImage+animatedGIF类