sathya
sathya

Reputation: 384

how to change images in iphone app dynamically through XML file

My app contains some images which needs to be dynamically loaded from web server through XML file. Every time when changes done in admin console at web server it should reflect in iPhone app too through the XML file. I wrote XML file but dont know how to get used in iPhone code. I referred the following links,

how-to-retrieve-data-through-xml-in-iphone-locally-file

dynamically-pulling-images-from-xml-for-iphone-app

how-to-change-the-tabbaritem-images-dynamically-in-iphone-app

retrieving-images-to-iphone-app-through-xml

but all these links are not clear for me to understand. Kindly suggest me a way to do it.

Upvotes: 1

Views: 624

Answers (1)

ilmiacs
ilmiacs

Reputation: 2576

So I assume your web server can serve an XML which includes the image in some encoded format like base64. Then you need to:

  1. Load the file. For this, you should use NSURLConnection, e.g. as described in how-to-make-http-request-from-iphone-and-parse-json-result
  2. Parse the XML. Instead of parsing a JSON, you need to parse your XML, so first, you need an XML-parser. Here is a comparison of XML parsers. Then you should access the element in the XML that represents your image. This step depends on your choice of the parser.
  3. Decode the Image. Your element is probably an NSString and you want to have its binary representation as NSData. NSData has some convenience functions for this. E.g. for base64, there is the dataFromBase64String: constructor.
  4. Create the image. That is, UIImage using the imageWithData: constructor.
  5. Display the image. You need an UIImageView and assign to it your UIImage. Set the frame of the view and add it to your preferred subview.

It is also possible that the XML provides URLs where to get the image data. Then you have to do a new request. This should be clear by now, how to do.

Good luck.

Upvotes: 1

Related Questions