用UIWebView加载本地图片和gif图

加载gif图:

 NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"9" ofType:@"gif"]];

    // view生成

    [webView loadData:gif MIMEType:@"image/gif" textEncodingName:NULL baseURL:NULL];

加载本地的图片:

 //加载本地的图片原理是使用webView加载html标签

  UIImage *selectedImage = [UIImage imageNamed:@"icon_VC6_boxStart"];

    NSString *stringImage = [self htmlForJPGImage:selectedImage];

    //构造内容

    NSString *contentImg = [NSString stringWithFormat:@"%@", stringImage];

    NSString *content =[NSString stringWithFormat:

                        @"<html>\n"

                        "<style type=\"text/css\">\n"

                        "<!--\n"

                        "body{font-size:40pt;line-height:60pt;}\n"

                        "-->\n"

                        "</style>\n"

                        "<body>\n"

                        "%@\n"

                        "</body>\n"

                        "</html>"

                        , contentImg];

    

    //让self.contentWebView加载content

    [webView loadHTMLString:content baseURL:nil];

//编码图片

- (NSString *)htmlForJPGImage:(UIImage *)image

{

    NSData *imageData = UIImageJPEGRepresentation(image,1.0);

    NSString *str1=[NSString stringWithFormat:@"data:image/jpg;base64,%@",[imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];

    return [NSString stringWithFormat:@"<img src = \"%@\" />", str1];

}

posted @ 2016-08-02 14:52  _Ace  阅读(265)  评论(0编辑  收藏  举报