1.导入头文件:
import WebKit
2.设置WKWebView
let gifView = WKWebView(frame: CGRect(x: 0, y: NaviHigh, width: ScreenWidth, height: ScreenHeight - NaviHigh - TabBarHigh))`
获取图片的路径并转换为URL
let urlStr = URL(fileURLWithPath: Bundle.main.path(forResource: "QY-LOAD", ofType: "gif")!)
下面若直接var gifData : Data = Data(contentsOf: urlStr)的话,会报错Call can throw, but it is not marked with 'try' and the error is not handled,原因是:在OC中[NSData dataWithContentsOfFile:@"路径" options:<#(NSDataReadingOptions)#> error:错误处理],和Swift对比发现Swift缺少错误处理,因此如果不加try...catch会报错,此处用try...catch处理下异常情况即可
var gifData : Data = Data()
do{
gifData = try Data(contentsOf: urlStr)
}
catch { }
gifView.isUserInteractionEnabled = false
gifView.load(gifData , mimeType: "image/gif", characterEncodingName: "UTF-8", baseURL: urlStr)
loadView.addSubview(gifView)`
网友评论