How to create an Image from byte array - iphone

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.

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];

Adding Image to plist [duplicate]

This question already has answers here:
Storing image in plist
(4 answers)
Closed 9 years ago.
I'm making an app where I take picture using camera and want to save those picture on plist so that i can use in future. I also want to retrive the saved images from plist and display them on imageview. There are multiple images.Please anyone can help me with this
Here is my code which I'm using for adding to plist
imageArray addObject:[NSString stringWithFormat:#"%#",imageView.image]];
[imageArray writeToFile:plistPath atomically:YES];
But when I'm trying to retrive image and set it on imageview I cant see image.
Thanks.
You can't save like this method to plist. The supported format are NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber. You can convert your UIImage into NSData and can write in plist.
To convert an image into NSData
NSData *data = UIImagePNGRepresentation(imageview.image);
[imageArray addObject:data];
[imageArray writeToFile:plistPath atomically:YES];
and also NSLog your file path to find where yoyr plist is saved.
Hope this helps !!!
see this one ,
For saving :
- (IBAction)saveImage {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
For retrieving:
- (IBAction)getImage {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
}

Replacing image URLs in html string

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

Storing images to the iphone

Is it possible to save images captured by the camera on the iphone to a custom directory or somewhere on the iphone rather than saving to the camera roll?
I would like to save images to the device and then have access to the at a later stage, rather than reading from the camera roll using ALAssets.
Is this possible?
Yes you can save them to your apps' documents folder. Here is an example:
// Create path
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/ImageName.png"];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
To retrieve it:
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
To list all files in a directory:
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"]];
for (NSString *s in fileList){
NSLog(s);
}
Use this to write
// imageData = NSData object
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *imageCacheDirPath = [docsPath stringByAppendingPathComponent:#"anyFolderName"];
NSString *imageCachePath = [imageCacheDirPath stringByAppendingPathComponent:#"image.jpg"];
[imageData writeToFile:imageCachePath atomically:YES];
And the following snippet to read the data again:
// Get path of image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *imageCacheDirPath = [docsPath stringByAppendingPathComponent:#"anyFolderName"];
NSString *imageCachePath = [imageCacheDirPath stringByAppendingPathComponent:#"image.jpg"];
NSData *imageData = [NSData dataWithContentsOfFile:imageCachePath];
Note that in this example the image is stored in a subfolder of your "Documents" folder and this folder is called "anyFolderName". The variable docsPath contains the Path to the "Documents" folder of your dir.
Note that this is only the code I use, its from iOS 3.0 times, there might be better ways to store an image to the Documents folder of your app.

Saving a video in the form of an NSData object to file

I am calling –writeToFile:atomically: on an NSData object which contains a video I just shot.
The file is not being written and if I use the version that returns an erorr object, the error is nil.
My code is:
if ([[info valueForKey:UIImagePickerControllerMediaType] isEqualToString:#"public.movie"]) {
NSURL* url = [info valueForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:#"capturedVideo.MOV"];
[videoData writeToFile:fPath atomically:YES];
}
The return value is NO and when I check the existence of the written file, it is not there.
Any ideas?
Try creating file with NSFileManager before writing to it.