IOS uses AFN for single and multi-image uploads

Source: Internet
Author: User

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

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.