Uploading images with afnetworking

Source: Internet
Author: User

In fact, I made a long time, did not make a lot of uploads together, had to get a single upload, if you see this article is helpful to you to welcome you to improve my information in a timely manner, but also let me learn how to upload more than how to get, thank you

First instantiate a Afhttprequestoperationmanager action object

Afhttprequestoperationmanager *manager = [[Afhttprequestoperationmanager alloc] Initwithbaseurl:[nsurl URLWithString : @ "Upload image interface"];

Nsarray *paths = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);
NSString *documentsdirectory = [[Paths objectatindex:0] stringbyappendingpathcomponent:@ "logo1.jpg"];
NSData *logojpg = [NSData datawithcontentsoffile:documentsdirectory];
UIImage * image = [UIImage imagewithdata:logojpg];

It's taken from the phone path.
Nsurl *filepath = [Nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:@ "avatar.jpg" OfType:nil]];

NSData *imagedata = uiimagejpegrepresentation (image, 1.0);//Compression

UIImage *image = [UIImage imagenamed:@" Avatar 1" ];NSData *data =uiimagepngrepresentation (image);                  //in the network development, when uploading a file, the file is not allowed to be overwritten, the file name is duplicate        //To resolve this issue,         //You can use the current system event as the file name at upload timeNSDateFormatter *formatter =[[NSDateFormatter alloc] init];         //set the time formatFormatter.dateformat =@" yyyymmddhhmmss" ;NSString *str =[Formatter stringfromdate:[nsdate Date]];NSString *filename = [NSString stringWithFormat:@" %@.png" , str];          /* This method is parameter 1. [Binary data] to be uploaded  2. [Field "file"] for processing files [upload.php] on the website c14> 3. [File name] to save on the server 4. Upload the file [mimeType]*            /

if (imageData) {
Nsdictionary * Dic1 = parameters required by the @{};//interface
Nsmutableurlrequest *request = [Manager.requestserializer
multipartformrequestwithmethod:@ "POST"
urlstring:@ "Http://yapi.chelehuo.net/upload"
Parameters:dic1
constructingbodywithblock:^ (id<afmultipartformdata> formData) {

[FormData Appendpartwithfiledata:imagedata
name:@ "Logo1"
filename:@ "Logo1.jpg"
mimetype:@ "Image/jpeg"];

}
Error:nil];

Afhttprequestoperation *operation = [Manager httprequestoperationwithrequest:request success:^ ( Afhttprequestoperation *operation, id responseobject) {
NSLog (@ "operation1:%@", responseobject);
Get the parameters you want.
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "failure upload ...");
}];
[Manager.operationqueue addoperation:operation];
Can see the progress of the upload
[Operation setuploadprogressblock:^ (Nsuinteger Byteswritten, Long long totalbyteswritten, long long Totalbytesexpectedtowrite) {

NSLog (@ "percentage:%f", totalbyteswritten*1.0/totalbytesexpectedtowrite);

}];
}

How to upload multiple images

Method One:

< Span style= "COLOR: #6122ae" >nsmutableurlrequest  *request = [[ afhttprequestserializer &NBSP; Serializer] multipartformrequestwithmethod: @" POST " &NBSP; urlstring: url  parameters:url parameter   constructingbodywithblock:^ ( id < afmultipartformdata > formData) {

for (int i = 0; i<arrayimage. Count; i++) {

UIImage *uploadimage = arrayimage[i];

[FormData appendpartwithfiledata:uiimagepngrepresentation(uploadimage)name: [ NSString stringWithFormat:@ "parameter%d", i+1] fileName:@ "Test.jpg" mimeType:@ "image/jpg"];

}

} Error:nil];


Afhttprequestoperation *opration = [[afhttprequestoperationalloc] Initwithrequest: request];

opration. Responseserializer. Acceptablecontenttypes = [nssetsetwithobject:@ "text/html" ];

[Opration setcompletionblockwithsuccess: ^ (afhttprequestoperation *operation, ID Responseobject) {


} Failure: ^ (afhttprequestoperation *operation, nserror *error) {


}

This multiple upload I'm a failure don't know why

Here are some examples of blogs I've seen on the Internet, and you can see

http://blog.csdn.net/liuwuguigui/article/details/40142739

Http://blog.sina.com.cn/s/blog_a776fc650102uyvb.html

Http://www.tuicool.com/articles/JFJrue

Uploading images with afnetworking

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.