简体   繁体   English

将数据上传到服务器IPhone

[英]Upload Data To Server IPhone

I want to develop an iPhone app that allows the User to take and save a photo within the app. 我想开发一个iPhone应用程序,允许用户在应用程序中拍摄并保存照片。 Furthermore the user only is allowed to upload the image to server if data like "title", "location" and "additional information" is entered. 此外,如果输入诸如“标题”,“位置”和“附加信息”的数据,则仅允许用户将图像上载到服务器。

If done so, the image and the belonging data should be uploaded to my webserver. 如果这样做,则应将图像和所属数据上传到我的网络服务器。

I have already build an app that retrieves data from a server, so building a connection will not be the problem. 我已经构建了一个从服务器检索数据的应用程序,因此构建连接不会成为问题。

Since I'm new to web servers my question is what would be the best way to upload images combined with custom information? 由于我是网络服务器的新手,我的问题是上传图像和自定义信息的最佳方式是什么?

What would be a good way to store the images and the belonging information? 存储图像和所属信息的好方法是什么? My guess would be a MySQL DB for the imformation (title, location, additional info, image link), and what about the images? 我的猜测是用于信息的MySQL数据库(标题,位置,附加信息,图像链接),以及图像怎么样? Where can I read about file handling on web servers? 我在哪里可以阅读有关Web服务器上的文件处理?

Thanks in advance 提前致谢

You have to create an API on the web server that the iPhone can contact in order to POST data to your web server. 您必须在iPhone可以联系的Web服务器上创建API,以便将数据发布到Web服务器。 You can simply create an NSURLConnection in order to build your packet to post the data from the iPhone app. 您只需创建一个NSURLConnection即可构建数据包,以便从iPhone应用程序发布数据。

Inside an NSURLConnection you can tell it to be a POST packet and then add data to the body of the request. 在NSURLConnection中,您可以将其称为POST数据包,然后将数据添加到请求正文中。 Your image data should be converted to UTF8 and stored as a nvarchar or something along those lines in your database. 您的图像数据应转换为UTF8并存储为nvarchar或数据库中这些行的内容。

Understand, this is an overview of what you have to do, without knowledge of your internal workings of the web app I cannot give you specifics. 理解,这是对您必须做的事情的概述,如果不了解您的Web应用程序的内部工作原理,我无法向您详细说明。

I would suggest converting the image to its base64 representation, and you can then post this in a web service call. 我建议将图像转换为base64表示,然后您可以在Web服务调用中发布它。

Firstly, convert the UIImage to NSData by using: 首先,使用以下命令将UIImage转换为NSData:

UIImage *img = [UIImage imageNamed:@"MyImage.png"];
NSData *imgData = UIImageJPEGRepresentation(img, 1.0);

You can then convert this to a base64 encoded string using some helpful classes created by Matt Gallagher - see this post here . 然后,您可以使用Matt Gallagher创建的一些有用的类将其转换为base64编码的字符串 - 请参阅此处的帖子 In the code available to download, there is an NSData+Base64 class that will allow you to convert the data to a string: 在可供下载的代码中,有一个NSData + Base64类,允许您将数据转换为字符串:

NSString *imgBase64 = [imgData base64EncodedString];

Once you have this string, you can post it to the server, and either store the string or decode it and save it on the server as an image file. 获得此字符串后,您可以将其发布到服务器,然后存储字符串或对其进行解码并将其作为映像文件保存在服务器上。 You should be able to find an example of how to do this using your chosen technology. 您应该能够找到如何使用您选择的技术执行此操作的示例。

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

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