Upload Data To Server IPhone

这一生的挚爱 提交于 2019-12-13 05:48:24

问题


I want to develop an iPhone app that allows the User to take and save a photo within the app. 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? Where can I read about file handling on web servers?

Thanks in advance


回答1:


You have to create an API on the web server that the iPhone can contact in order to POST data to your web server. You can simply create an NSURLConnection in order to build your packet to post the data from the iPhone app.

Inside an NSURLConnection you can tell it to be a POST packet and then add data to the body of the request. Your image data should be converted to UTF8 and stored as a nvarchar or something along those lines in your database.

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.




回答2:


I would suggest converting the image to its base64 representation, and you can then post this in a web service call.

Firstly, convert the UIImage to NSData by using:

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. In the code available to download, there is an NSData+Base64 class that will allow you to convert the data to a string:

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.



来源:https://stackoverflow.com/questions/9520970/upload-data-to-server-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!