Swift3:在UIWebView中加载GIF
Swift 3: Load GIF in UIWebView
我想在我的 swift 3 应用程序中加载 GIF。我用这段代码来做到这一点,但没有显示 gif。
let filePath = Bundle.main.path(forResource: “myGif”, ofType: "gif")
let gif = NSData(contentsOfFile: filePath!)
self.webView.load(gif! as Data, mimeType: "image/gif", textEncodingName: String(), baseURL: NSURL() as URL)
self.webView.isUserInteractionEnabled = false
我不知道我的代码有什么问题(我是 swift 和 iOS 开发的新手)。
有什么帮助吗?
我从 github 中找到 SwiftGif Lbrary,它不使用 webview(如果你想使用 webview,请写信给我评论以更新答案)
编辑:
网络视图:
let url = Bundle.main.url(forResource: "source", withExtension: "gif")!
let data = try! Data(contentsOf: url)
webview.load(data, mimeType: "image/gif", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
webview.scalesPageToFit = true
webview.contentMode = UIViewContentMode.scaleAspectFit
希望对您有所帮助。
这是工作示例。我从网上下载了 gif 图片并将其添加到示例项目中。
let url = Bundle.main.url(forResource: "hsk", withExtension: "gif")!
let data = try! Data(contentsOf: url)
webView.load(data, mimeType: "image/gif", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
通过上面的代码,我可以看到如下的动画 gif。
我想在我的 swift 3 应用程序中加载 GIF。我用这段代码来做到这一点,但没有显示 gif。
let filePath = Bundle.main.path(forResource: “myGif”, ofType: "gif")
let gif = NSData(contentsOfFile: filePath!)
self.webView.load(gif! as Data, mimeType: "image/gif", textEncodingName: String(), baseURL: NSURL() as URL)
self.webView.isUserInteractionEnabled = false
我不知道我的代码有什么问题(我是 swift 和 iOS 开发的新手)。
有什么帮助吗?
我从 github 中找到 SwiftGif Lbrary,它不使用 webview(如果你想使用 webview,请写信给我评论以更新答案)
编辑:
网络视图:
let url = Bundle.main.url(forResource: "source", withExtension: "gif")!
let data = try! Data(contentsOf: url)
webview.load(data, mimeType: "image/gif", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
webview.scalesPageToFit = true
webview.contentMode = UIViewContentMode.scaleAspectFit
希望对您有所帮助。
这是工作示例。我从网上下载了 gif 图片并将其添加到示例项目中。
let url = Bundle.main.url(forResource: "hsk", withExtension: "gif")!
let data = try! Data(contentsOf: url)
webView.load(data, mimeType: "image/gif", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
通过上面的代码,我可以看到如下的动画 gif。