This solution builds on the answer from Ammar Hasan. Instead of the original...
NSData *data = [NSData dataWithContentsOfURL:imageURL];
[self.webView loadData:data MIMEType:@"image/jpeg" textEncodingName:@"utf-8" baseURL:nil];
...you can do this:
NSString *photoWrapper = [NSString stringWithFormat:@"\
<html>\n\
<head>\n\
<meta name=\"viewport\" content=\"width=device-width\" />\n\
<style type=\"text/css\">\n\
body {\n\
margin: 0;\n\
padding: 0;\n\
}\n\
img {\n\
width: 100%%;\n\
}\n\
</style>\n\
</head>\n\
<body>\n\
<img src=\"%@\" />\n\
</body>\n\
</html>\
", URL];
[self.documentView loadHTMLString:photoWrapper baseURL:nil];
Now the image is scaled to the width of the web view, even when the web view is less than half the width of the image, which seems to be a limit that UIWebView imposes otherwise. The image can still be scaled by the user, which I wanted. You can override that by adding minimum-scale
and maximum-scale
to the viewport meta tag.