Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

iphone - Fit image of random size into a UIWebview (IOS)

I want to fit large image into UIwebview with keeping image ratio same as image view.

How can i do it.?

My code as follows to fit image in Uiwebview. if image is large then display is not good.

                CGFloat screenWidth = self.view.frame.size.width;
                CGFloat screenHeight = self.view.frame.size.height;

                NSString *htmlString = [NSString stringWithFormat:@"%@", @"<html><head><meta name='viewport' content='user-scalable=yes,width=device-width'></head><body bgcolor='000000'><img src='%@' width='%f' height='%f' style='max-width:200% max-height:200%'></body></html>"];
                imageHTML  = [[NSString alloc] initWithFormat:htmlString, fileUrl, screenWidth, screenHeight];

        [Webview loadHTMLString:imageHTML baseURL:nil];
        [imageHTML release];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...