IOS Image Upload

Source: Internet
Author: User
// The Demarcation Line identifier nsstring * identifier = @ "aab03x"; // initialize the request nsmutableurlrequest * request = [nsmutableurlrequest requestwithurl: [nsurl urlwithstring: url] cachepolicy: Required timeinterval: 10]; // demarcation line -- aab03x nsstring * mpboundary = [[nsstring alloc] initwithformat: @ "-- % @", twitterfon_form_boundary]; // Terminator aab03x -- nsstring * endmpboundary = [[nsstring alloc] Initwithformat: @ "% @ --", mpboundary]; // The image uiimage * image to be uploaded = [Params objectforkey: @ "pic"]; // obtain the image's data nsdata * Data = uiimagepngrepresentation (image); // The HTTP body string nsmutablestring * Body = [[nsmutablestring alloc] init]; // nsarray * keys = [Params allkeys]; // traverses keys for (INT I = 0; I <[Keys count]; I ++) {// obtain the current key nsstring * Key = [Keys objectatindex: I]; // if the key is not pic, the value is of the character type, such as name: Boris If (! [Key isequaltostring: @ "pic"]) {// Add a line break, line feed [Body appendformat: @ "% @ \ r \ n", mpboundary]; // Add the field name, for two rows [Body appendformat: @ "content-Disposition: Form-data; name = \ "% @ \" \ r \ n ", key]; // Add the field value [Body appendformat: @ "% @ \ r \ n", [Params objectforkey: Key] ;}/// add a line break, line feed [Body appendformat: @ "% @ \ r \ n", mpboundary]; // specifies a picic field. The file name is boris.png [Body appendformat: @ "content-Disposition: Form-data; name = \" pic \"; filename = \ "boris.png \" \ r \ n "]; // declare the format of the uploaded file [Body appendformat: @" Content-Type: image/PNG \ r \ n "]; // declaration Terminator: -- aab03x -- nsstring * end = [[nsstring alloc] initwithformat: @ "\ r \ n % @", endmpboundary]; // declare myrequestdata to put the HTTP body nsmutabledata * myrequestdata = [nsmutabledata data]; // convert the body string to a binary string in utf8 format [myrequestdata appenddata: [Body datausingencoding: nsutf8stringencoding]; // Add the image data to [myrequestdata appenddata: Data]; // Add the terminator -- aab03x -- [myrequestdata appenddata: [end datausingencoding: nsutf8stringencoding]; // set the value of Content-Type in httpheader nsstring * content = [[nsstring alloc] initwithformat: @ "multipart/form-data; boundary = % @", twitterfon_form_boundary]; // set httpheader [Request setvalue: Content forhttpheaderfield: @ "Content-Type"]; // set Content-Length [Request setvalue: [nsstring stringwithformat: @ "% d ", [myrequestdata length] forhttpheaderfield: @ "Content-Length"]; // set HTTP body [Request sethttpbody: myrequestdata]; // HTTP method [Request sethttpmethod: @ "Post"]; // establish a connection and set the proxy nsurlconnection * conn = [[nsurlconnection alloc] initwithrequest: Request delegate: Self]; // set the data if (conn) {mresp ****** edata = [[nsmutabledata data] retain] to accept resp ***** E;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.