Load local images in a local HTML file within a UIWebView
The problem is,
Your app can't figure out the where your resources are, and where your html is.
Why? Because your html and resources are added in bundle, which is scattered. You need to put all in single folder, so that your resources are accessible. According to your code
<img src="../media/sjolyst.jpg" alt="Sjølyst"/>
You are trying to fetch the image relatively, but in actual folder structure there is no ../media
.
Solution simple.
- Delete all your html thing, I guess the folder.
- Re-add the HTML folder, see image
You should see a folder like image below
You are now good to go.
By adding a folder mentioned in 2. creates a folder in bundle path, and put all the resource with correct folder structure inside it.
Hope this helps.
Cheers.
Using HTML and Local Images Within UIWebView
Using relative paths or file: paths to refer to images does not work with UIWebView. Instead you have to load the HTML into the view with the correct baseURL:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
You can then refer to your images like this:
<img src="myimage.png">
(from uiwebview revisited)
Loading external image in local HTML inside UIWebView
I don't really understand what you are trying to do, but if you want to show an image from a URL you can do that easily with a UIImageView
like this:
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://www.stackoverflow.com/image.jpg"]]];
Or if you want a UIWebView
you could do a separate HTML file and load it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView setScalesPageToFit:YES];
Or you can test to add tags like html, body etc.
I'm open for more info about your problem.
iOS WebView remote html with local image files
Ok here is an example how to subclass NSURLProtocol and deliver an image (image1.png) which is already in the bundle. Below is the subclasses' header, the implementation as well as an example how to use it in a viewController(incomplete code) and a local html file(which can be easily exchanged with a remote one). I've called the custom protocol: myapp://
as you can see in the html file at the bottom.
And thanks for the question! I was asking this myself for quite a long time, the time it took to figure this out was worth every second.
EDIT:
If someone has difficulties making my code run under the current iOS version, please have a look at the answer from sjs. When I answered the question it was working though. He's pointing out some helpful additions and corrected some issues, so give props to him as well.
This is how it looks in my simulator:
MyCustomURLProtocol.h
@interface MyCustomURLProtocol : NSURLProtocol
{
NSURLRequest *request;
}
@property (nonatomic, retain) NSURLRequest *request;
@end
MyCustomURLProtocol.m
#import "MyCustomURLProtocol.h"
@implementation MyCustomURLProtocol
@synthesize request;
+ (BOOL)canInitWithRequest:(NSURLRequest*)theRequest
{
if ([theRequest.URL.scheme caseInsensitiveCompare:@"myapp"] == NSOrderedSame) {
return YES;
}
return NO;
}
+ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)theRequest
{
return theRequest;
}
- (void)startLoading
{
NSLog(@"%@", request.URL);
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[request URL]
MIMEType:@"image/png"
expectedContentLength:-1
textEncodingName:nil];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"image1" ofType:@"png"];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[[self client] URLProtocol:self didLoadData:data];
[[self client] URLProtocolDidFinishLoading:self];
[response release];
}
- (void)stopLoading
{
NSLog(@"something went wrong!");
}
@end
MyCustomProtocolViewController.h
@interface MyCustomProtocolViewController : UIViewController {
UIWebView *webView;
}
@property (nonatomic, retain) UIWebView *webView;
@end
MyCustomProtocolViewController.m
...
@implementation MyCustomProtocolViewController
@synthesize webView;
- (void)awakeFromNib
{
self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(20, 20, 280, 420)] autorelease];
[self.view addSubview:webView];
}
- (void)viewDidLoad
{
// ----> IMPORTANT!!! :) <----
[NSURLProtocol registerClass:[MyCustomURLProtocol class]];
NSString * localHtmlFilePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"html"];
NSString * localHtmlFileURL = [NSString stringWithFormat:@"file://%@", localHtmlFilePath];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:localHtmlFileURL]]];
NSString *html = [NSString stringWithContentsOfFile:localHtmlFilePath encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:html baseURL:nil];
}
file.html
<html>
<body>
<h1>we are loading a custom protocol</h1>
<b>image?</b><br/>
<img src="myapp://image1.png" />
<body>
</html>
Load image in local HTML file -- UIWebView
Instead of loading with an NSURLRequest
, read the html file into an NSString
and create an NSURL
to the directory with image files.
Then use UIWebView's - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
method.
So, you'd have something like...
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:html baseURL:[url URLByDeletingLastPathComponent]];
How to load images with HTML file in UIWebView
HTML File:
<HTML>
<HEAD>
<TITLE>My Web Page</TITLE>
</HEAD>
<BODY>
<img alt="Sample Image" src="1.jpg" style="width: 100px; height: 100px;"/>
<P>This is where you will enter all the text and images you want displayed in a browser window.</P>
</BODY>
</HTML>
webView code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"desk" ofType:@"html"];
NSURL *url=[NSURL fileURLWithPath:htmlFile];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
screenshot:
UIWebView with local HTML file not showing image
Have you checked the URL? It might be a relative one (e.g. '/img/foo.jpg').
You can add a base URL to your webview in that case:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
Loading a local image directly into a UIWebView without HTML?
UIWebView
has a loadData:MIMEType:textEncodingName:baseURL:
method. Rather than writing HTML with the image in it, you could do something simpler like this
NSData *imageData = [NSData dataWithContentsOfURL:fileURL];
[self.webView loadData:imageData MIMEType:@"image/..." textEncodingName:@"utf-8" baseURL:nil];
the MIMEType
argument should be changed to whatever MIME type the image is. (e.g. jpeg is image/jpeg
)
Related Topics
Do I Use ≪Img≫, ≪Object≫, or ≪Embed≫ For Svg Files
Make Grid Container Fill Columns Not Rows
How to Set Subject/Content of Email Using Mailto:
How to Keep Origin in Center of Image in Scale Animation
Twitter Bootstrap Navbar Fixed Top Overlapping Site
Bootstrap Full-Width With 2 Different Backgrounds (And 2 Columns)
How to Use Content-Disposition For Force a File to Download to the Hard Drive
Disable Auto Zoom in Input "Text" Tag - Safari on Iphone
Force Flex Item to Span Full Row Width
Display Pdf Within Web Browser
With Ng-Bind-Html-Unsafe Removed, How to Inject Html
Change Color of Png Image Via Css
Float:Left; VS Display:Inline; VS Display:Inline-Block; VS Display:Table-Cell;
How to Disable the Resizable Property of a Textarea
How to Force a Web Browser Not to Cache Images
How to Change Bootstrap 3 Column Order on Mobile Layout