通过XML将图像检索到iphone应用程序 [英] Retrieving images to iphone app through XML

查看:88
本文介绍了通过XML将图像检索到iphone应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个iphone应用程序,通过xml保存从远程服务器检索数据。我想在XMl中检索图像,并且必须在我存储在服务器上的iphone应用程序中显示它们。请帮助如何实现这一目标。

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.

推荐答案

在我的应用程序中,我通过Xml发送和接收图像。为了促进这一点,我使用base64编码对图像进行编码/解码。 MY编码/解码方法有2个Objective-c类别,1个用于NSData,1个用于NSString。您可以在相应的数据类型上调用这些方法,我在下面列出了示例。方法是[NSData base64Encoding]和[NSString base64Decoding]。

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];

我的b64String变量现在有一个base64编码的图像字符串表示。您现在可以将它包装在Xml中并使用普通的NSURLConnection将其发送到服务器。

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.

从服务器接收时...(确保发送base64编码)来自服务器的文本)

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

你将进行正常的Xml解析,这里我的value变量保存来自服务器的base64字符串。

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];

你应该能够在Objective-C中查找一些常见的base64编码/解码算法,让我吧知道你是否希望我发布我的方法(它们有点长,所以我在这里注意了它们)。

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).

这篇关于通过XML将图像检索到iphone应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆