简体   繁体   English

通过XML将图像检索到iPhone应用程序

[英]Retrieving images to iphone app through XML

I have made an iphone application in which data is retrieved from remote server through xml saved. 我制作了一个iPhone应用程序,其中通过保存的xml从远程服务器检索数据。 I want to retrieve images also with in the XMl and have to show them in my iphone application stored on server. 我也想在XMl中检索图像,并且必须在存储在服务器上的iphone应用程序中显示它们。 Please help how this can be achieved. 请帮助如何实现。

In my application I am both sending and receiving images through Xml. 在我的应用程序中,我同时通过Xml发送和接收图像。 In order to facilitate this, I encode/decode the images using base64 encoding. 为了简化此操作,我使用base64编码对图像进行编码/解码。 MY encode/decode methods live in 2 objective-c categories, 1 for NSData and 1 for NSString. MY编码/解码方法分为2个Objective-C类别,其中1个用于NSData,另外1个用于NSString。 You call these methods on the respective data types, I have listed examples below. 您在各自的数据类型上调用了这些方法,下面列出了示例。 The methods are [NSData base64Encoding] and [NSString base64Decoding]. 方法是[NSData base64Encoding]和[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. 我的“ b64String”变量现在具有我的图像的base64编码的字符串表示形式。 You can now wrap this in Xml and use your normal NSURLConnection to send it to the server. 现在,您可以将其包装在Xml中,并使用常规的NSURLConnection将其发送到服务器。

When receiving from the sever... (Make sure you are sending base64 encoded text from the server) 从服务器接收时...(确保从服务器发送base64编码的文本)

You will do your normal Xml parsing, here my "value" variable holds the base64 string from the server. 您将进行常规的Xml解析,这里我的“值”变量保存了服务器的base64字符串。

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). 您应该能够在Objective-C中查找一些常见的base64编码/解码算法,让我知道您是否要发布我的方法(它们有点长,因此我在这里省略了它们)。

I'm writing an app at the moment that has this problem - I've solved it by putting a url to the image in the xml instead of embedding the image data itself. 目前,我正在编写一个有此问题的应用程序-通过将url放置到xml中而不是嵌入图像数据本身来解决了该问题。 For example my xml looks something like this : 例如,我的xml看起来像这样:

<images>
  <image url="http://www.example.com/images/12345.jpg" />
  <image url="http://www.example.com/images/67890.jpg" />
</images>

As I'm parsing the xml, when I hit an < image > tag I store the url in an NSMutalbeArray. 在解析xml时,当我点击<image>标记时,会将URL存储在NSMutalbeArray中。 When the xml is parsed I loop through the array and get each image using NSURLConnection. 解析xml后,我遍历数组并使用NSURLConnection获取每个图像。

Sam 山姆

PS I worked out how to use NSURLConnection from this page : https://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html PS我从此页面上了解了如何使用NSURLConnection: https : //developer.apple.com/mac/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

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

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