小秋的家

home

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  1 Posts :: 91 Stories :: 17 Comments :: 0 Trackbacks

UIWebView加载的html里,加载工程文件的图片 

第一种方法:

       在info.plist里面定义个URL schema比如叫 youapp,那么路径就写成youapp://test.png

可以参照http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html

第二种方法:

       NSString *htmlString = @"<html><head><body><img src=\"logo.png\"><body></head></html>";
       NSString *path = [[NSBundle mainBundle] bundlePath];
       NSURL *baseURL = [NSURL fileURLWithPath:path];
       [self.myWebView loadHTMLString:htmlString baseURL:baseURL];

这个html是你自己写的一个本地html文件,用来布置UIWebView界面。里面用到的图片可以是工程文件里面的,也可以是网络上的。给出Url。

解决在UIWebView中加载图片时出现延迟现象的做法

文字和图片同时显示:
NSString* showHtml = @"<html><head></head><body><img src='data:image/jpg;base64,%@'/><p>here is show a picture.</p></body></html>"; NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"www.abc.com/img/img1.jpg"]]; NSString* imageString = [imageData base64Encoding];  UIWebView* webView = [UIWebView alloc]; [webView loadHTMLString:[NSString stringWithFormat:showHtml, imageString] baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; //以下把该UIWebView显示出来的代码略

粗体的地方就是通过base64来实现的地方,我们这里显示的是jpg格式的图片,因此在img标签的src属性里面写的是“src=’data:image/jpg;base64,%@’”,而如果是其他格式的图片的话记得把对应的地方给改一下

用Javascript判断图片是否存在,不存在则显示默认图片

<img src="http://www.iecn.net/images/logo_home.gif" onerror="javascript:this.src='http://www.cnlei.org/BLOG/styles/style2007/images/logo.gif'" />
posted on 2011-10-09 11:33 棋剑小秋 阅读(1013) 评论(0)  编辑  收藏 所属分类: iPhone

只有注册用户登录后才能发表评论。


网站导航: