Fit Image of Random Size into a Uiwebview (Ios)

Fit image of random size into a UIWebview (IOS)

I have used below code and its working well.

   NSString *imageHTML = [[NSString alloc] initWithFormat:@"%@%@%@", @"<!DOCTYPE html>"
"<html lang=\"ja\">"
"<head>"
"<meta charset=\"UTF-8\">"
"<style type=\"text/css\">"
"html{margin:0;padding:0;}"
"body {"
"margin: 0;"
"padding: 0;"
"color: #363636;"
"font-size: 90%;"
"line-height: 1.6;"
"background: black;"
"}"
"img{"
"position: absolute;"
"top: 0;"
"bottom: 0;"
"left: 0;"
"right: 0;"
"margin: auto;"
"max-width: 100%;"
"max-height: 100%;"
"}"
"</style>"
"</head>"
"<body id=\"page\">"
"<img src='",fileUrl,@"'/> </body></html>"];

[wview_contents loadHTMLString:imageHTML baseURL:nil];

image size after applying aspect fit for the image in an UIWebView

Here is the solution that iOS provides to find the actual size after rendering the image view in the given rectangle..

Assume origImgSize is actual physical image size and targetImgSize is the rectangular area of UIImageView on the screen..

CGRect sizeBeingScaledTo = AVMakeRectWithAspectRatioInsideRect(origImgSize, CGRectMake(0, 0, targetImgSize.width, targetImgSize.height));

This gives the actual size of the image after rendering using AspectFit mode.



Related Topics



Leave a reply



Submit