I want to show a large image on UIWebViewso that the user sees the full image when he comes to this screen. The webview should be scaled so that it occupies the entire width of the image and adapts it to the available screen width (the width / height of the image should remain unchanged). Therefore, web browsing should be reduced.
Therefore, I used the property scalesPageToFit. The image is about 3900x2613, but you can use another large image . It is also interesting that the user cannot completely zoom out manually to see the whole image.
Now I have created a test project and tried various solutions for uploading images (locally, web resource, NSData, ...). Now I am giving you an example of C # code.
Locally:
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;
Or from a web resource:
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")));
Or with local image and 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);
You can, of course, provide code examples in Objective-C / Swift for answers! In my real project, I work with NSData, where I get a string from webservice. I also tried some solutions for various similar posts on SO, but scaling did not help.
If you enter the URL in Safari, the webview seems to be doing it right. But why not in my code?