简体   繁体   English

如何通过XML文件动态更改iPhone应用程序中的图像

[英]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. 我的应用程序包含一些图像,需要通过XML文件从Web服务器动态加载这些图像。 Every time when changes done in admin console at web server it should reflect in iPhone app too through the XML file. 每次在Web服务器的管理控制台中进行更改时,它也应该通过XML文件反映在iPhone应用程序中。 I wrote XML file but dont know how to get used in iPhone code. 我写了XML文件,但不知道如何在iPhone代码中使用它。 I referred the following links, 我引用了以下链接,

how-to-retrieve-data-through-xml-in-iphone-locally-file 如何通过iPhone本地文件中的xml检索数据

dynamically-pulling-images-from-xml-for-iphone-app 从xml动态为iPhone应用程序拉图像

how-to-change-the-tabbaritem-images-dynamically-in-iphone-app 如何在iPhone应用程序中动态更改Tabbaritem图像

retrieving-images-to-iphone-app-through-xml 通过xml将图像检索到iPhone应用程序

but all these links are not clear for me to understand. 但是所有这些链接都不清楚,我无法理解。 Kindly suggest me a way to do it. 请给我一种方法。

So I assume your web server can serve an XML which includes the image in some encoded format like base64. 因此,我假设您的Web服务器可以提供XML,其中包括以base64之类的某种编码格式的图像。 Then you need to: 然后,您需要:

  1. Load the file. 加载文件。 For this, you should use NSURLConnection , eg as described in how-to-make-http-request-from-iphone-and-parse-json-result 为此,您应该使用NSURLConnection ,例如,如如何制作来自iphone的HTTP请求和解析json结果中所述
  2. Parse the XML. 解析XML。 Instead of parsing a JSON, you need to parse your XML, so first, you need an XML-parser. 除了解析JSON外,还需要解析XML,因此首先需要一个XML解析器。 Here is a comparison of XML parsers . 这是XML解析器的比较 Then you should access the element in the XML that represents your image. 然后,您应该访问表示图像的XML中的元素。 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 . 您的元素可能是NSString并且您希望将其二进制表示为NSData NSData has some convenience functions for this. NSData为此提供了一些便利功能。 Eg for base64, there is the dataFromBase64String: constructor. 例如,对于base64,有dataFromBase64String:构造函数。
  4. Create the image. 创建图像。 That is, UIImage using the imageWithData: constructor. 也就是说, UIImage使用imageWithData:构造函数。
  5. Display the image. 显示图像。 You need an UIImageView and assign to it your UIImage . 您需要一个UIImageView并为其分配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. XML也可能提供从何处获取图像数据的URL。 Then you have to do a new request. 然后,您必须执行一个新请求。 This should be clear by now, how to do. 现在应该清楚该怎么做。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM