Iphone – Retrieving images to iphone app through XML

imageiphonexml

I have made an iphone application in which data is retrieved from remote server through xml saved. I want to retrieve images also with in the XMl and have to show them in my iphone application stored on server. Please help how this can be achieved.

Best Answer

In my application I am both sending and receiving images through Xml. In order to facilitate this, I encode/decode the images using base64 encoding. MY encode/decode methods live in 2 objective-c categories, 1 for NSData and 1 for NSString. You call these methods on the respective data types, I have listed examples below. The methods are [NSData base64Encoding] and [NSString base64Decoding].

When sending to the server...

NSLog(@"Compressing Image: JPEG 80%");
NSData *imgData = UIImageJPEGRepresentation(scaledImage, 0.8f);    
NSString *b64String = [imgData base64Encoding];

My "b64String" variable now has a base64 encoded string representation of my image. You can now wrap this in Xml and use your normal NSURLConnection to send it to the server.

When receiving from the sever... (Make sure you are sending base64 encoded text from the server)

You will do your normal Xml parsing, here my "value" variable holds the base64 string from the server.

NSData *imgData = [value base64Decoding];
UIImage *image = [[UIImage alloc] initWithData:imgData];

You should be able to look up some common base64 encoding/decoding algorithms in Objective-C, let me know if you would like me to post my methods (they are kinda long so I obmitted them here).