tes*_*ing 5 image zoom scale uiwebview ios
我想在a上显示一个大图像,UIWebView以便用户在看到这个屏幕时看到完整的图像.webview应采用这样的比例,即它占据图像的整个宽度并根据可用的屏幕宽度进行调整(图像的比例宽度/高度应保持不变).所以webview必须缩小.
因此我使用了这个scalesPageToFit属性.图像大约为3900x2613,但您也可以使用其他大图像.有趣的是,用户无法手动完全缩小以查看整个图像.
现在我做了一个测试项目并尝试了不同的解决方案来加载图像(本地,Web资源,NSData,......).现在我在C#中为您提供一些示例代码.
本地:
webView = new UIWebView (View.Bounds);
View.AddSubview (webView);
string filePath = NSBundle.MainBundle.PathForResource ("sample", "jpg");
NSUrl fileURL = new NSUrl (filePath);
webView.LoadRequest (new NSUrlRequest (fileURL));
webView.ScalesPageToFit = true;
Run Code Online (Sandbox Code Playgroud)
或者来自网络资源:
webView = new UIWebView (View.Bounds);
webView.ScalesPageToFit = true;
View.AddSubview (webView);
webView.LoadRequest (new NSUrlRequest (new NSUrl("http://www.libpng.org/pub/png/img_png/16million-pschmidt.png")));
Run Code Online (Sandbox Code Playgroud)
或者使用本地图像和HTML:
webView = new UIWebView ();
webView.TranslatesAutoresizingMaskIntoConstraints = false;
webView.ScalesPageToFit = true;
View.AddSubview (webView);
NSMutableDictionary viewsDictionary = new NSMutableDictionary ();
viewsDictionary ["webView"] = webView;
View.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|[webView]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
View.AddConstraints (NSLayoutConstraint.FromVisualFormat ("V:|[webView]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
NSUrl baseUrl = new NSUrl (NSBundle.MainBundle.BundlePath, true);
string fileName = "some.jpg";
string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
string webViewContent = String.Format ("<html><body><img src=\"{0}\" width=\"100%\"/></body></html>", localDocUrl);
webView.LoadHtmlString (webViewContent, baseUrl);
Run Code Online (Sandbox Code Playgroud)
您当然可以在Objective-C/Swift中提供代码示例以获得答案!在我的真实项目中,我正在使用NSData,在那里我从webservice获取一个字符串.我也在SO上尝试了一些不同类似帖子的解决方案,但是缩放仍然是错误的.
如果您在Safari中输入URL,webview似乎可以正常运行.但为什么不在我的代码?