在我的 iOS 应用程序中使用 Google WebP 图像格式时,我发现 Google 的库擅长在本机代码中显示 WebP 图像。
现在我也想在我的 webapp 中使用 WebP,但 UIWebView 不支持。有什么想法可以实施吗?
如果您使用的是 Cordova/Phonegap,有一个插件可以将 WebP 支持添加到 UIWebView。如果您不使用 Cordova,您应该能够使用相同的机制而无需插件注册。
您可以使用 Pod https://github.com/rs/SDWebImage
POD:-pod 'SDWebImage/WebP'
使用 Swift 4.2:
let thumbimgurl = URL(string: "https://res.cloudinary.com/demo/image/upload/w_300/sample.webp")
SDWebImageManager.shared().loadImage(with: thumbimgurl, options: .highPriority, progress: nil) { (DisplayUIImage, ImageSize, Error, SDImageCacheType, ImageBool, ImageURL) in
self.MyImageDisplayImageView.image = DisplayUIImage
}
您可以使用 Pod https://github.com/rs/SDWebImage
从 Web 加载 WebP 图像真的很容易
使用斯威夫特:
SDWebImageManager.shared().loadImage(with: url, options: .highPriority, progress: nil, completed: {(resultSet) in
YOURWEBVIEW.image = resultSet.0
})
如果您的资源在您的包中,您可以使用SDWebImage库以这种方式执行此操作:
let webpData: Data = try Data(contentsOf: Bundle.main.url(forResource: "your_resource_name", withExtension:"webp")!)
imageView.image = SDImageWebPCoder.shared.decodedImage(with: webpData, options: nil)