views:

446

answers:

2

Hi All!! 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.

+1  A: 

Have a look at the functions UIImageJPEGRepresentation and UIImagePNGRepresentation.

epatel
+1  A: 

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.

Roger Nolan