我从服务器获取.svg网址。如何在我的应用程序中将其显示为图像。我试图在UIWebView中显示它但我无法调整内容图像的大小。我正在使用此代码
let request: NSURLRequest = NSURLRequest(url: URL(string: "https://upload.wikimedia.org/wikipedia/en/c/c3/Flag_of_France.svg")!)
webView.scalesPageToFit = true
webView.contentMode = .scaleAspectFit
webView.loadRequest(request as URLRequest)
我的问题是什么。
答案 0 :(得分:1)
解决方案是更正Web视图的滚动视图比例属性以适应其边界。
http://yberdnikov.github.io/2014/06/18/uiwebview-and-svg-images-scaling/
所以在快速,你会做这样的事情:
import UIKit
// make sure to include the UIWebViewDelegate protocol
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet var webView : UIWebView?
override func viewDidLoad() {
super.viewDidLoad()
let request: NSURLRequest = NSURLRequest(url: URL(string: "https://upload.wikimedia.org/wikipedia/en/c/c3/Flag_of_France.svg")!)
// don't forget to set the webView's delegate to self
webView?.delegate = self
// disable scrolling
webView?.scrollView.isScrollEnabled = false
webView?.scalesPageToFit = true
webView?.contentMode = .scaleAspectFit
webView?.loadRequest(request as URLRequest)
// set webView's background color to clear, sometimes a black line appears at the bottom since the svg doesn't cover the entire view.
webView?.backgroundColor = UIColor.clear
}
// function from delegate protocol
func webViewDidFinishLoad(_ webView: UIWebView) {
let contentSize = webView.scrollView.contentSize
let webViewSize = webView.bounds.size
let scaleFactor = webViewSize.width / contentSize.width
// scale the svg appropriately
webView.scrollView.minimumZoomScale = scaleFactor
webView.scrollView.maximumZoomScale = scaleFactor
webView.scrollView.zoomScale = scaleFactor
}
}