UIWebview - Loading PNG image - got error - ios

I am trying to load the image(png) in UIWebview. But I got the following error
""ImageIO: PNGinvalid literal/lengths set"
I will get the "png" from the web service. I will store it in the following path. "Users/XXX/Library/Application Support/iPhone Simulator/4.3.2/Applications/E4DE3097-EA84-492F-A2A7-5882202F3B00/Documents/attachement.png "
When I reading the file from the path, I got the error. I am using the following code
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *commentHtml = [NSString stringWithFormat:#"<img src=\"%#\"/>", documentEntity.path];
NSString *html3 = [NSString stringWithFormat:#"<html><head/><body><div><b>COMMENTS:</b></div>%#</body></html>", commentHtml];
[self.documentView loadHTMLString:html3 baseURL:baseURL];
Please help me.
THanks
Girija

If you have saved the Image in Documents Folder, why are you fetching from MainBundle.
Fetching Image from the Documents Directory would be :
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:#"%#/attachement.png", aDocumentsDirectory];
UIImage *anImage = [UIImage imageWithContentsOfFile:aFilePath];

Related

How to set image into uiwebview?

I have created a html code like see above and the code uploaded to http://www.ged.Ic.com site.
Added an image name “1.jpg” into Xcode project.
When open iPad app loading http://www.ged.Ic.com/page/ from webview.
How to get to set image from into Xcode project to img src="image path from xcode"
NSString *imageFileName = #"image";
NSString *imageFileExtension = #"png";
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageFileName ofType:imageFileExtension];
NSString *imgHTMLTag = [NSString stringWithFormat:#"<img src=\"file://%#\" />", imagePath];
NSString *imageUrlString = #"http://www.gstatic.com/translate/intl/ru/logo.png";
NSString *yourText = #"Some text here";
NSString *htmlString = [NSString stringWithFormat:#"<img src='%#'/><br><b>%#</b>", imageUrlString, yourText];
[myWebView loadHTMLString:htmlString baseURL:nil];

Xcode app run on simulator but not on device

i've saw a lot of topic but no one answers to my questions.
This Program run on simulator but when i executed it , it crash .
This is my simple code. Thanks you!
NSURL *url=[NSURL fileURLWithPath:#"/Users/marco/Desktop/Letters/1.txt"];
NSString *fileContent = [NSString stringWithContentsOfURL:url];
NSLog(#"fileContent = %#", fileContent);
NSArray * a = [NSArray arrayWithObjects:fileContent, nil];
Output.text=[a objectAtIndex:0];
NSLog (#"The 4th integer is: %#", a);
Move your 1.txt file to your device, for example your documents folder.
Then change the first line of your app like so:
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [NSString stringWithFormat:#"%#/1.txt", documentsPath];
NSURL *url = [NSURL fileURLWithPath:filePath];
Since you say this works on the simulator, I'll have to assume you added this file to your project properly. If not, add the file to your project and target. You can get the URL to any resource you included at build time through NSBundle:
NSBundle *bundle = [NSBundle bundleForClass:[self class]]; // often you will see [NSBunble mainBundle, but both work
NSURL *url = [bundle URLForResource:#"1" withExtension:#"txt"];

NSURL is always nil dispite encoding

I'm generating a PDF file and am attempting to preview it as shown below, but URL routinely returns NIL despite my formatting (which is what seems to resolve everyone else's issue with this common problem). I must be missing something more. Any ideas?
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [path objectAtIndex:0];
docDirectory = [docDirectory stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [[NSBundle mainBundle] URLForResource:docDirectory withExtension:#"pdf"];
if (URL) {
// Initialize Document Interaction Controller
self->documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self->documentInteractionController setDelegate:self];
// Preview PDF
[self->documentInteractionController presentPreviewAnimated:YES];
}
Your path creation seems all out of whack. Try something like this:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [path objectAtIndex:0];
NSString *filename = #"myfile.pdf"; // replace with the actual filename you used
NSString *fullPath = [docDirectory stringByAppendingPathComponent:filename];
NSURL *fullURL = [NSURL fileURLWithPath:fullPath];
In the code you posted you don't provide a filename. You have the Documents directory and the pdf extension. And there is no need to "percent escape" the URL in this case.
You are probably confused of how to obtain the path to your file: NSSearchPathForDirectoriesInDomains is used to get an absolute path to (for example) your documents directory. NSBundle's URLForResource:withExtension: on the other hand searches the app wrapper for a file with the provided name.
Mixing the two would do no good. You should probably just look into the documents directory.

editing occurrences is not working

i am trying to get the path to a certain file
and then open it in a webview. so i need to replace each space by '%20'
NSString *test=#"filename";
NSString *finalPath12 = [test stringByAppendingString:#".pdf"];
NSString *path1 = [[NSBundle mainBundle] bundlePath];
NSString *finalPath1 = [path1 stringByAppendingPathComponent:finalPath12];
NSString *file =#"file://";
NSString *htmlfilename1 = [file stringByAppendingString:finalPath1];
NSString *pathtofile = [htmlfilename1 stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
any string is working except #"%20".
this works perfectly for exemple:
NSString *pathtofile = [htmlfilename1 stringByReplacingOccurrencesOfString:#" " withString:#"string"];
but i need the #"%20". What am i missing ? Thanks
There is already [NSString stringByAddingPercentEscapesUsingEncoding:] (reference) for that very purpose.
However in your case, as you want a URL, you can replace all the lines in your question with:
NSURL *url = [[NSBundle mainBundle] urlForResource:#"filename" withExtension:#"pdf"];
You need to use #"%%20".
As first % is treated as escape/wild character.
Or use
stringByAddingPercentEscapesUsingEncoding:

Split View App, display PDF's in the Detail View, PDF's are local storage

I have a Split View App that I am building, using a plist for my Array, and using a UIWebView to display content in the DetailViewController. Using the below code to make calls to the URL address in my plist, and the URL Address is displaying a PDF in the DetailViewController through the UIWebView.
DetailViewController.m
NSString *mutareURL = [_detailItem description];
NSURL *url = [NSURL URLWithString:mutareURL];
[_catalog loadRequest:[NSURLRequest requestWithURL:url]];
What I need to do is have all of the PDF's in my resource folder in my project. I can use an NSArray or plist for the RootViewController (TableView), but I need to have the ability to display the PDF's in the DetailViewController through the UIWebView when a Row is selected in the RootViewController (TableView). I tried this Code in my DetailViewController.m file, but it is not working.
NSString *path = [[NSBundle mainBundle] pathForResource:#"%#" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[_Pdf loadRequest:request];
I have an NSLog in my RootViewController.m file with this code, and i do see that the names of the pdf files are showing up in my debugger. I just do not know how to get them from the RootViewController and pass them into the pathforResourse"#"%#" ofType:#"pdf"]; area. brochures is an NSArray that has all of the addObject:#"nameofpdfs" that is passing.
RootViewController.m
detailViewController.detailItem = [NSString stringWithFormat:#"%#", [brochures objectAtIndex:indexPath.row]];
NSLog(#"Cell created for row: %d", [indexPath row]);
NSLog(#"brochures = %#", detailViewController.detailItem);
Thank you
The answer is probably in the last snippet. The first line essentially passes the selected filename from the RootViewController to the DetailViewController by setting the latter's detailItem property.
If you replace
NSString *path = [[NSBundle mainBundle] pathForResource:#"%#" ofType:#"pdf"];
by
NSString *path = [[NSBundle mainBundle] pathForResource: self.detailItem ofType:#"pdf"];
it should work.
Depending on whether the filenames in the brochures array already contain the #".pdf" suffix, you might have to leave it out of the message send:
NSString *path = [[NSBundle mainBundle] pathForResource: self.detailItem ofType: nil];

Resources