IOS uploads multiple pictures at once and returns progress values

Source: Internet
Author: User

I encapsulated a method that uses afnetworking to upload multiple graphs. With progress bar callback, I hope to help you, my app in the pro-test is available, do not know how your server receives data:

/** * Upload content with pictures, allow multiple images upload (URL) post** @param URL Network request address * @param images to upload an array of images (note that the array content needs to be a picture) *               @param parameter The corresponding parameters of the image array (note multiple pictures, each picture name should be different) * @param parameters Other parameters dictionary * @param ratio Picture compression ratio (between 0.0~1.0) * @param succeedblock Successful callback * @param Failedblock failed callback * @param uploadprogressblock Callback for Progress*/+(void) Startmultipartuploadtaskwithurl: (NSString *) URL Imagesarray: (Nsarray*) Images parameterofimages: (NSString*) parameter parametersdict: (Nsdictionary*) Parameters Compressionratio: (float) ratio Succeedblock: (void(^) (IDOperation,IDresponseobject)) Succeedblock Failedblock: (void(^) (IDOperation, Nserror *error)) Failedblock Uploadprogressblock: (void(^) (floatUploadpercent,Long LongTotalbyteswritten,Long Longtotalbytesexpectedtowrite)) Uploadprogressblock; Achieve:+(void) Startmultipartuploadtaskwithurl: (NSString *) URL Imagesarray: (Nsarray*) Images parameterofimages: (NSString*) parameter parametersdict: (Nsdictionary*) Parameters Compressionratio: (float) ratio Succeedblock: (void(^) (ID,ID)) Succeedblock Failedblock: (void(^) (ID, Nserror *)) Failedblock Uploadprogressblock: (void(^) (float,Long Long,Long Long)) uploadprogressblock{if(Images.count = =0) {NSLog (@"uploaded content does not contain pictures"); return; }     for(inti =0; i < Images.count; i++) {        if(! [Images Iskindofclass:[uiimageclass]]) {NSLog (@"the%d element in images is not a UIImage object", i+1); return; }} afhttprequestoperation*operation = [[Self Sharedoperation].operationmanager post:url parameters:parameters constructingbodywithblock:^ (ID<AFMultipartFormData>formData) {                inti =0; //generate picture names based on current system timeNSDate *date =[NSDate Date]; NSDateFormatter*formatter =[[NSDateFormatter alloc]init]; [Formatter Setdateformat:@"yyyy mm month DD Day"]; NSString*datestring =[Formatter stringfromdate:date];  for(UIImage *imageinchimages) {NSString*filename = [NSString stringWithFormat:@"%@%d.png", Datestring,i]; NSData*ImageData; if(ratio >0.0f&& ratio <1.0f) {ImageData=uiimagejpegrepresentation (image, ratio); }Else{imageData= UIImageJPEGRepresentation (Image,1.0f); } [FormData appendpartwithfiledata:imagedata name:parameter filename:filename mimeType:@"Image/jpg/png/jpeg"]; }} Success:^ (Afhttprequestoperation *operation,IDresponseobject)            {Succeedblock (operation,responseobject); } Failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"%@", error);            Failedblock (Operation,error);        }]; [Operation Setuploadprogressblock:^ (Nsuinteger Byteswritten,Long LongTotalbyteswritten,Long Longtotalbytesexpectedtowrite) {CGFloat percent= Totalbyteswritten *1.0/Totalbytesexpectedtowrite;    Uploadprogressblock (Percent,totalbyteswritten,totalbytesexpectedtowrite);    }]; }

IOS uploads multiple pictures at once and returns progress values

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.