Reprinted from: Http://www.jianshu.com/p/0e28fdef0f91
Images must be compressed when uploading, otherwise it will fail to upload
1. Single image upload
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager manager]; [Manager post:urlstring Parameters:params constructingbodywithblock:^ (id_nonnull formData) {
Use date to generate picture name
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
Formatter.dateformat = @ "Yyyy-mm-dd HH:mm:ss";
NSString *filename = [NSString stringwithformat:@ "%@.png", [Formatter stringfromdate:[nsdate Date]];
[FormData appendpartwithfiledata:imagedata name:@ "UploadFile" Filename:filename mimetype:@ "Image/png"];
} success:^ (Afhttprequestoperation * _nonnull operation, id _nonnull responseobject) {
Upload image successfully executed callback
Completion (RESPONSEOBJECT,NIL);
} failure:^ (Afhttprequestoperation * _nonnull operation, Nserror * _nonnull error) {
Upload image failed to execute callback
Completion (NIL,ERROR);
}];
2. Multi-image upload
The difference between multi-image uploads and single-image uploads is the file name
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager manager]; [Manager post:urlstring Parameters:params constructingbodywithblock:^ (id_nonnull formData) {
Nsinteger imgcount = 0;
For (NSData *imagedata in Imagedatas) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
Formatter.dateformat = @ "Yyyy-mm-dd HH:mm:ss:SSS";
NSString *filename = [NSString stringwithformat:@ "%@%@.png", [Formatter stringfromdate:[nsdate date]],@ (ImgCount)];
[FormData appendpartwithfiledata:imagedata name:[nsstring stringwithformat:@ "uploadfile%@", @ (Imgcount)] FileName:filename mimetype:@ "Image/png"];
imgcount++;
}
} success:^ (Afhttprequestoperation * _nonnull operation, id _nonnull responseobject) {
Completion (RESPONSEOBJECT,NIL);
} failure:^ (Afhttprequestoperation * _nonnull operation, Nserror * _nonnull error) {
Completion (NIL,ERROR);
}];
IOS uses AFN for single and multi-image uploads