1

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
  • What format is the image data sent as? If you receive a JPG you can only save it as a JPG unless you convert it to a different format. You cannot simply choose what format you want, it needs to be the format the image you received is. Commented Jun 5, 2009 at 7:15

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at the functions UIImageJPEGRepresentation and UIImagePNGRepresentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.