|
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: Statement: /** * Upload content with pictures, allow multiple images to upload (URL) POST * * @param URL Network request address * @param images An array of images to upload (note that the array contents need to be pictures) * @param parameter The corresponding parameters of the image array * @param Parameters Other parameters Dictionary * @param compression ratio of ratio pictures (between 0.0~1.0) * @param succeedblock a successful callback * @param Failedblock failed callback * @param callback for Uploadprogressblock upload progress */ + (void) Startmultipartuploadtaskwithurl: (NSString *) URL Imagesarray: (Nsarray *) images Parameterofimages: (NSString *) parameter Parametersdict: (nsdictionary *) parameters Compressionratio: (float) ratio Succeedblock: (void (^) (ID operation, id responseobject)) succeedblock Failedblock: (void (^) (ID operation, Nserror *error)) Failedblock Uploadprogressblock: (void (^) (float Uploadpercent,long long Totalbyteswritten,long long Totalbytesexpectedtowrite)) Uploadprogressblock;
Realize: + (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 (@ "Upload content does not contain images"); Return } for (int i = 0; i < Images.count; i++) { if (![ ImagesIskindofclass:[uiimage class]]) { NSLog (@ "Images%d element is not a UIImage object", i+1); Return } }
Afhttprequestoperation *operation = [[Self Sharedoperation].operationmanager post:url parameters:parameters constructingbodywithblock:^ (id<afmultipartformdata> formData) {
int i = 0; Generate picture names based on current system time NSDate *date = [NSDate Date]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [Formatter setdateformat:@ "yyyy mm month DD Day"]; NSString *datestring = [Formatter stringfromdate:date];
For (UIImage *image in images) { 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, id responseobject) { Succeedblock (Operation,responseobject);
} failure:^ (Afhttprequestoperation *operation, Nserror *error) { NSLog (@ "%@", error); Failedblock (Operation,error);
}];
[Operation setuploadprogressblock:^ (Nsuinteger Byteswritten, Long long totalbyteswritten, long long Totalbytesexpectedtowrite) { CGFloat percent = Totalbyteswritten * 1.0/totalbytesexpectedtowrite; Uploadprogressblock (Percent,totalbyteswritten,totalbytesexpectedtowrite); }];
}
|
|
IOS-(multi-image upload encapsulated)