Upload images on iOS

Source: Internet
Author: User
The following describes how to upload images:

-(Void) LoadImage :( nsstring *) aurl
{
Nsdata * imagedata;
Nsmutabledata * postbody;
Nsstring * stringboundary, * contenttype;
Nsurl * url = [nsurl urlwithstring: aurl]; // convert the string to nsurl format

Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0];
Nsstring * Path = [documentsdirectory stringbyappendingpathcomponent: @ "1.jpg"];

Imagedata = [[nsdata alloc] initwithcontentsoffile: path];

Asiformdatarequest * arequest = [[asiformdatarequest alloc] initwithurl: url];

Stringboundary = [nsstring stringwithstring: @ "0 xkhtmlboundary"];

Contenttype = [nsstring stringwithformat: @ "multipart/form-data; boundary =%@", stringboundary];

[Arequest addrequestheader: @ "Content-Type" value: contenttype];

Postbody = [[nsmutabledata alloc] init];

[Postbody appenddata: [[nsstring stringwithformat: @ "\ r \ n -- % @ \ r \ n", stringboundary] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithstring: @ "content-Disposition: Form-data; name = \" title \ "\ r \ n"] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithstring: @ "lighttable"] datausingencoding: nsutf8stringencoding]; // so light table show up as source in Twitter post

Nsstring * imagefilename = [nsstring stringwithformat: @ "photo.jpeg"];

[Postbody appenddata: [[nsstring stringwithformat: @ "\ r \ n -- % @ \ r \ n", stringboundary] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithformat: @ "content-Disposition: Form-data; name = \" Upload \"; filename = \ "% @ \" \ r \ n ", imagefilename] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [@ "Content-Type: image/JPEG \ r \ n" datausingencoding: nsutf8stringencoding];

[Postbody appenddata: imagedata];

[Postbody appenddata: [[nsstring stringwithformat: @ "\ r \ n -- % @ \ r \ n", stringboundary] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithstring: @ "content-Disposition: Form-data; name = \" password \ "\ r \ n"] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithstring: @ "lighttablexxxxxxxx"] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithformat: @ "\ r \ n -- % @ \ r \ n", stringboundary] datausingencoding: nsutf8stringencoding];

[Arequest setdelegate: Self];

[Arequest appendpostdata: postbody];

[Arequest setrequestmethod: @ "Post"];

[Arequest startasynchronous];

[Postbody release];
[Arequest release];
}

 

The following describes how to upload images:

-(Void) LoadImage :( nsstring *) aurl
{
Nsdata * imagedata;
Nsmutabledata * postbody;
Nsstring * stringboundary, * contenttype;
Nsurl * url = [nsurl urlwithstring: aurl]; // convert the string to nsurl format

Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0];
Nsstring * Path = [documentsdirectory stringbyappendingpathcomponent: @ "1.jpg"];

Imagedata = [[nsdata alloc] initwithcontentsoffile: path];

Asiformdatarequest * arequest = [[asiformdatarequest alloc] initwithurl: url];

Stringboundary = [nsstring stringwithstring: @ "0 xkhtmlboundary"];

Contenttype = [nsstring stringwithformat: @ "multipart/form-data; boundary =%@", stringboundary];

[Arequest addrequestheader: @ "Content-Type" value: contenttype];

Postbody = [[nsmutabledata alloc] init];

[Postbody appenddata: [[nsstring stringwithformat: @ "\ r \ n -- % @ \ r \ n", stringboundary] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithstring: @ "content-Disposition: Form-data; name = \" title \ "\ r \ n"] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithstring: @ "lighttable"] datausingencoding: nsutf8stringencoding]; // so light table show up as source in Twitter post

Nsstring * imagefilename = [nsstring stringwithformat: @ "photo.jpeg"];

[Postbody appenddata: [[nsstring stringwithformat: @ "\ r \ n -- % @ \ r \ n", stringboundary] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithformat: @ "content-Disposition: Form-data; name = \" Upload \"; filename = \ "% @ \" \ r \ n ", imagefilename] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [@ "Content-Type: image/JPEG \ r \ n" datausingencoding: nsutf8stringencoding];

[Postbody appenddata: imagedata];

[Postbody appenddata: [[nsstring stringwithformat: @ "\ r \ n -- % @ \ r \ n", stringboundary] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithstring: @ "content-Disposition: Form-data; name = \" password \ "\ r \ n"] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithstring: @ "lighttablexxxxxxxx"] datausingencoding: nsutf8stringencoding];

[Postbody appenddata: [[nsstring stringwithformat: @ "\ r \ n -- % @ \ r \ n", stringboundary] datausingencoding: nsutf8stringencoding];

[Arequest setdelegate: Self];

[Arequest appendpostdata: postbody];

[Arequest setrequestmethod: @ "Post"];

[Arequest startasynchronous];

[Postbody release];
[Arequest release];
}

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.