UIWebView - 3

来源:互联网 发布:flexikey软件如何卸载 编辑:程序博客网 时间:2024/06/11 11:12

UIWebView怎么读取本地的html? iOS提供了直接的接口,但是要读取一张图片,例如

通常我们没有网络的提醒图片,参考:

http://blog.csdn.net/cuibo1123/article/details/38496025

先写个网络检测:

+ (BOOL)isExistenceNetwork{        BOOL isExistenceNetwork;    SYReachability *reachability = [SYReachability reachabilityForInternetConnection];        switch([reachability currentReachabilityStatus]) {        case NotReachable:            isExistenceNetwork = FALSE;            break;        case ReachableViaWWAN:            isExistenceNetwork = TRUE;            break;        case ReachableViaWiFi:            isExistenceNetwork = TRUE;            break;    }    return  isExistenceNetwork;}


写一个category来帮助将UIImage转化为html编码:

#import "NSString+Helper.h"@implementation NSString (SYSDKHelper)+ (NSString *)htmlForJPGImage:(UIImage *)image{    NSData *imageData = UIImageJPEGRepresentation(image,1.0);    NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[imageData base64Encoding]];    return [NSString stringWithFormat:@"<img src = \"%@\" />", imageSource];}@end



最后将它HTML转码化:

UIImage *selectedImage = UIImageFromSDKResourceBundle(@"sy_webview_neterror", @"png");NSString *stringImage = [NSString htmlForJPGImage:selectedImage];NSString *contentImg = [NSString stringWithFormat:@"%@", stringImage];NSString *content =[NSString stringWithFormat:                    @"<html>"                    "<head>"                    "<meta charset='utf-8'>"                    "<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'>"                    "<style> html,body{ height: 100%; width: 100%; } img{ width:300; height: 300; position: absolute;}"                    "</style>"                    "</head>"                    "<body>"                    "%@"                    "</body>"                    "</html>"                    , contentImg];[self.myWebView loadHTMLString:content baseURL:nil];


这样就行了。




0 0