I have an image file saved in the application's temp folder, say IMG.png. I am injecting an HTML string into a UIWebView using:
NSString *HTML = [NSString stringWithFormat:
@"<body><h2>%@</h2><img src=\"%@\"></img><p>%@</p>%@</body>",
title, image, body];
[webView loadHTMLString:HTML baseURL:[NSURL URLWithString:HOST]];
I tried many combinations of paths for the NSString image
, but I cannot get this WebView to load my local image properly.
Can you please help me?
Here's what I use to save my image in the temp folder:
[self saveImage:result withFileName:post.title ofType:@"jpg" inDirectory:NSTemporaryDirectory()];
which relies on:
-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
} else {
NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
}
}