iOS uses HTTP to upload image implementation methods

Source: Internet
Author: User

if (parameters) {

int gendernumber = 1;
Self.token = Logintoken;
SELF.PERSONPK = KIDPK;
Self.personname = Personnamel;
Self.personnickname = Nicknamel;
Self.gender = Genderl;
Self.birthday = Birthdayl;
Self.imagedata = Imagel;
Self.imagename = Imagenamel;
if ([Self.gender isequaltostring:@ "female"]) {

Gendernumber = 2;
}

[Parameters SetValue:self.token forkey:@ "token"];
[Parameters SetValue:self.personPK forkey:@ "PERSONPK"];
[Parameters setValue:self.personName forkey:@ "Person.name"];
[Parameters setValue:self.personNickName forkey:@ "Person.nickname"];
[Parameters setvalue:[nsstring stringwithformat:@ "%d", Gendernumber] forkey:@ "Person.gender"];
[Parameters setValue:self.birthday forkey:@ "Birthday"];
[Parameters setValue:self.personNickName forkey:@ "Turfilefilename"];
[Parameters setvalue:image forkey:@ "Turfile"];


}

Self. Loadstatus = LOADING;
Self.strurl = tmp;
NSLog (@ "The URL is%@", self.strurl);
Self.activedownload = [Nsmutabledata data];


Identifier of the dividing line
NSString *twitterfon_form_boundary = @ "aab03x";
Initialize request based on URL
nsmutableurlrequest* Requestl = [nsmutableurlrequest requestwithurl:[nsurl URLWithString:self.strURL]
Cachepolicy:nsurlrequestreloadignoringlocalcachedata
TIMEOUTINTERVAL:30];
Dividing line--aab03x
NSString *mpboundary=[[nsstring alloc]initwithformat:@ "--%@", twitterfon_form_boundary];
Terminator aab03x--
NSString *endmpboundary=[[nsstring alloc]initwithformat:@ "%@--", mpboundary];
Get the image data
Self.imagedata = uiimagepngrepresentation (image);
The string of the HTTP body
Nsmutablestring *body=[[nsmutablestring Alloc]init];
A collection of all keys for the collection of parameters
Nsarray *keys= [Parameters AllKeys];

//Traverse keys Note that this removes the data type except that it contains a string of type
for (int i=0;i<[keys count];i++)
{
Get current key
NSString *key=[keys OBJECTATINDEX:I];
If key is not a pic, it indicates that value is a character type, such as Name:boris
Add dividing line, wrap
[Body appendformat:@ "%@\r\n", mpboundary];
Add field name, change 2 lines
[Body appendformat:@ "content-disposition:form-data; name=\"%@\ "\r\n\r\n", key];
Add a value for a field
[Body appendformat:@ "%@\r\n", [parameters Objectforkey:key]];

}

//Note that the turfile here is actually the name of the file to be uploaded
Add dividing line, wrap
[Body appendformat:@ "%@\r\n", mpboundary];
DECLARE pic field with file name Boris.png
[Body appendformat:@ "content-disposition:form-data; name=\"Turfile\"; Filename=\ "boris.jpg\" \ r \ n "];
Declaring the format of the uploaded file
[Body appendformat:@] content-type:image/JPEG\r\n\r\n "];//This is because the previous compression generated a JPG-type picture so you need to indicate JPEG, if PNG is PNG

Declaration Terminator:--aab03x--
NSString *end=[[nsstring alloc]initwithformat:@ "\r\n%@", endmpboundary];
Declare Myrequestdata, which is used to put the HTTP body
Nsmutabledata *myrequestdata=[nsmutabledata data];
Converts the body string to a binary in the UTF8 format
[Myrequestdata appenddata:[body datausingencoding:nsutf8stringencoding];
Add the data of image to
[Myrequestdata AppendData:self.imageData];
Join 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
[Requestl setvalue:content forhttpheaderfield:@ "Content-type"];
Set Content-length
[Requestl setvalue:[nsstring stringwithformat:@ "%d", [myrequestdata length]] forhttpheaderfield:@ "Content-Length"];
Set HTTP body
[Requestl Sethttpbody:myrequestdata];
HTTP method
[Requestl sethttpmethod:@ "POST"];

Establish a connection, set up an agent
Nsurlconnection *conn = [[Nsurlconnection alloc] Initwithrequest:requestl delegate:self];
Self.loadconnection = conn;
[Conn release];

if (self.loadconnection! = nil)
{
[Self.loadconnection Scheduleinrunloop:[nsrunloop Currentrunloop] formode:nsrunloopcommonmodes];
}

The method of compression is implemented:

-(void) compraseimage
{
UIImage *largeimage = self.pictureViewUpload.image;

Double compressionratio = 1;
int resizeattempts = 5;

NSData * Imgdata = uiimagejpegrepresentation (largeimage,compressionratio);

NSLog (@ "Starting Size:%i", [imgdata length]);

Trying to push it below around about 0.4 meg
while ([Imgdata length] > 400000 && resizeattempts > 0) {
Resizeattempts-= 1;

NSLog (@ "Image was bigger than 400000 Bytes. resizing. ");
NSLog (@ "%i attempts Remaining", resizeattempts);

Increase the compression amount
Compressionratio = compressionratio*0.5;
NSLog (@ "Compressionratio%f", compressionratio);
Test size before compression
NSLog (@ "Current Size:%i", [imgdata length]);
Imgdata = UIImageJPEGRepresentation (largeimage,compressionratio);

Test size after compression
NSLog (@ "New Size:%i", [imgdata length]);
}

Set Image by comprssed version
Self.pictureView.image = [UIImage imagewithdata:imgdata];

Check How big the image was now it been compressed and put into the Uiimageview

I made change here, you were again storing it with highest Resolution * * *
NSData *enddata = uiimagejpegrepresentation (largeimage,compressionratio);
NSLog (@ "Ending Size:%i", [enddata length]);

/* NSString *path = [self createpath:@ "myimage.jpg"];
NSLog (@ "%@", Path);
[Enddata Writetofile:path atomically:yes];*/

[Self startuploadingrequestwithdata:enddata andImage:self.pictureViewUpload.image];
}

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.