Replacing image URLs in html string - iphone

Hello I have a html string, with tags for every image.
I can extract the url of every image, but what I want is to save those images in the device, and then replace each one's image url with the corresponding path from my sandbox and then display them via UIWebView.
Any ideas how do I do that?
Thank you!

Well if you have the URL for an image, you can save it like this
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL urlWithString:string]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"MyFile"];
[imageData writeToFile:filePath atomically:YES];
To replace the URLs in the HTML string simply use the method
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement

Related

Save uiimage with custom format

I want to save an image with ".wai" extension, for send it trough whatsapp.h
ow can I save the image with this format?
The whatsapp site don't have a sample code, for more information this is the link (propbably visiting this link you understand my problem)
http://www.whatsapp.com/faq/en/iphone/23559013
NSData *pngData = UIImagePNGRepresentation(drawimg.image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
filePath = [documentsPath stringByAppendingPathComponent:#"edited.wai"]; //Add the file name
[pngData writeToFile:filePath atomically:YES];

AirPrint HTML file made locally iPhone

I am currently creating a html file and storing it locally, i give the user the choice of sending the html by HTML or print.
For some reason it is failing to print. if i test -
if([UIPrintInteractionController canPrintData:myData]){
it doesn't get past this point.
Is it possible to print local HTML files if so how to go about it.
Dan
edit code to create html -
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *Daily = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.html",self.title]];
[urlToLoad writeToFile:Daily atomically:YES encoding:NSUTF8StringEncoding error:NULL];
NSString *fileName = [NSString stringWithFormat:#"%#/%#.html", documentsDirectory,self.title];
NSData *myData = [NSData dataWithContentsOfFile:fileName];
[UIPrintInteractionController canPrintData:] is expecting the data to contain properly formatted PDF data, not HTML.
Try doing this instead:
UIPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:html];
[[UIPrintInteractionController sharedPrintController] setPrintFormatter:formatter];

Adding images into the iPhone application

I have an application where the user should be able to take pictures using camera and save them within the application. By default all the images taken are saved to the photo album.But I want to bring them into my project just like the way we add required images for the project into the Resources folder in Xcode. At the same time I want them to converted into PNG format.
After you have taken image from Camera you can save this image to your application's document directory as,
UIImage *image = imageFromCamera;
NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:#"MyImage.png"];
[imageData writeToFile:fullPathToFile atomically:YES];
Its tested code and working absolutely fine.
here is a great tutorial that you should check out http://iosdevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html
And here is the documentation from Apple for the UIImagePickerController that you should use https://developer.apple.com/documentation/uikit/uiimagepickercontroller
NSArray *UsrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocsDir = [UsrDocPath objectAtIndex:0];
NSString *path = [DocsDir stringByAppendingPathComponent:#"image.jpg"];
//Check if image exists on documents path, if exists removed it, but it depends on your logic, you can add more images in documents path by changing its name...
BOOL success = [FileManager fileExistsAtPath:zipPath];
if(success){
[FileManager removeItemAtPath:path error:&error];
}
[UIImageJPEGRepresentation(image,1.0) writeToFile:path atomically:YES];

UIWebView: Absolute path for images in app documents folder

I have a UIWebView that loads up some html. In it is an img tag, I am saving an image in the app documents folder and I want to display that image within the html, I do so like this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *tempImgFN = [NSString stringWithFormat:#"tempImg%d.jpg",storyRowID];
NSString *tempImg = [documentsDir stringByAppendingPathComponent:tempImgFN];
[imageBlob writeToFile:tempImg atomically:NO];
tempImg = [tempImg stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
tempImg = [tempImg stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
[string appendString:[NSString stringWithFormat:#"<img style='max-width:120px' src=\"%#\" alt=\"\"/>",tempImg]];
the imageBlob is an NSdata with the data for the image. the image is saved successfully and rest of the html loads up fine but the image is not displayed.
This works fine if I set the baseUrl of the UIwebView to point to the documents folder. But I don't want to do that because I want the baseURL to be [NSBundle mainBundle] resourcePath] to access some javascript files and css
woops that was stupid of me .. just needed to put src=\"file:/%#\"

How to create an Image from byte array

M stuck M trying to create an UIImage from a byte array which i get from a webservice.It comes embedded in a XML.I can parse the XML and get the byte array as a string.Then I convert the byte array (which is in NSString) to NSData. This is the code for that:-
Image_Content = currentElementValue;
NSData* aDataImage;
aDataImage = [Image_Content dataUsingEncoding: NSASCIIStringEncoding];
UIImage *Image_From_Array = [[UIImage alloc] initWithData:aData];
Can I save this file in certain format as jpg or png?
I want to save this image as a file so i can later use it in the HTML (this image is linked in the HTML).
Can any body help?
Thanks.
To write your data to the filesystem, use:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* fileName = [NSString stringWithFormat:#"%#/%#", documentsDirectory, aFileName];
BOOL writeSuccess = [imageData writeToFile:fileName atomically:NO];
Then to reload it later do the same:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* fileName = [NSString stringWithFormat:#"%#%#", documentsDirectory, aFileName];
image = [UIImage imageWithContentsOfFile:fileName];
You can also change your complression type by creating your image and generating the imageData using UIImageJPEGRepresentation and UIImagePNGRepresentation to generate the data you write to the filesystem.
Have a look at the functions UIImageJPEGRepresentation and UIImagePNGRepresentation.