Multi-image upload

Source: Internet
Author: User

From Post http://www.cocoachina.com/bbs/read.php?tid-285911-page-1.html

/**
* 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);
}];

}

Multi-image upload

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.