How can I load an image in iphone webview from a .html file?
如何从.html文件加载iphone webview中的图像?
2 个解决方案
#1
Easy
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];
NSLog(@"webview %@", bundlePath);
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
NSString * html = [NSString stringWithContentsOfFile:filePath encoding:NSStringEncodingConversionAllowLossy error:nil];
if (htmlData) {
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:bundleBaseURL];
}
Now in your html you can simply refer to the image as
现在在您的HTML中,您可以简单地将图像称为
#2
Yes. UIWebView
has two methods: -loadHtmlString:baseURL:
and -loadData:MIMEType:textEncodingName:baseURL:
是。 UIWebView有两个方法:-loadHtmlString:baseURL:和-loadData:MIMEType:textEncodingName:baseURL:
You can get the file data using NSData's -initWithContentsOfFile:
, then pass it to the latter of those methods above.
您可以使用NSData的-initWithContentsOfFile:获取文件数据,然后将其传递给上述方法中的后者。
EDIT: Whoops, I didn't notice the img part. But it still should work. Are you trying to load only the image?
编辑:哎呀,我没注意到img部分。但它仍然应该工作。你想加载图像吗?
#1
Easy
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];
NSLog(@"webview %@", bundlePath);
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
NSString * html = [NSString stringWithContentsOfFile:filePath encoding:NSStringEncodingConversionAllowLossy error:nil];
if (htmlData) {
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:bundleBaseURL];
}
Now in your html you can simply refer to the image as
现在在您的HTML中,您可以简单地将图像称为
#2
Yes. UIWebView
has two methods: -loadHtmlString:baseURL:
and -loadData:MIMEType:textEncodingName:baseURL:
是。 UIWebView有两个方法:-loadHtmlString:baseURL:和-loadData:MIMEType:textEncodingName:baseURL:
You can get the file data using NSData's -initWithContentsOfFile:
, then pass it to the latter of those methods above.
您可以使用NSData的-initWithContentsOfFile:获取文件数据,然后将其传递给上述方法中的后者。
EDIT: Whoops, I didn't notice the img part. But it still should work. Are you trying to load only the image?
编辑:哎呀,我没注意到img部分。但它仍然应该工作。你想加载图像吗?