Ios--afnetworking uploading images to the server

Source: Internet
Author: User

Tag:ica   option    response    nsfile   pre    upload    effects     traverse    manager   

filepath  is the path to upload to the server picture, if it is a picture, you can create a new image object, to obtain a path, such as a known UIImage object Image:cgsize size=image.size; nsdata *data=[self imagewithimage:image scaledtosize:sise]; uiimage *image1=[uiimage imagewithdata:data];               //Picture saved Path   nsstring * documentspath = [ Nshomedirectory ()  stringbyappendingpathcomponent:@ "Documents";     NSFileManager *fileManager = [NSFileManager defaultManager];     [filemanager createdirectoryatpath:documentspath withintermediatedirectories :yes attributes:nil error:nil];     [filemanager createfileatpath:[documentspath stringbyappendingstring:@ "/ Image.png "] contents:data attributes:nil];     nsstring *filepath = [[nsstring alloc]initwithformat:@ "%@%@", documentspath,  @ "/image.png"];     [self upload:filePath image:image1];   the yoururl  in the method Change to your own can be used, because afnetworking  is asynchronous   so I will share the function behind the method written in the success inside the   -  (void) Upload: (nsstring*) Filepath image: (uiimage*) img{            // form requests, uploading files      afhttpsessionmanager *manager = [afhttpsessionmanager  Manager];      manager.requestserializer = [afjsonrequestserializer serializer];//Request      manager.responseserializer = [afhttpresponseserializer serializer];//Response      manager.requestserializer.timeoutinterval = 20;      [manager.responseserializer setacceptablecontenttypes:[nsset setwithobjects: @ "Application/json",  @ "Text/json",  @ "Text/javascript", @ "textml", @ "Text/css", @ "Text/plain",  @ " Application/javaScript "@" Application/json ",  @" application/x-www-form-urlencoded ", nil]];     /*      * here need special attention, because there is no specific upload address, so this upload method is not successful, but the method is correct, Need to replace the correct upload address      */     nsdictionary *param=[nsdictionary  dictionarywithobjectsandkeys:@ "Picture", @ "paramname", filepath,@ "file",nil];       [manager post:@ "Yoururl"  parameters:param constructingbodywithblock:^ (id< Afmultipartformdata> _nonnull formdata)   {        // Upload pictures as forms          NSData *data1=[NSData  Datawithcontentsoffile:filepath];          nsdata *imgdata=uiimagepngrepresentation (IMG);
   [formdata appendpartwithfiledata:imgdata name:@ "Picture"  fileName:filePath  mimetype:@ "Image/png"];              } progress:^ (nsprogress * uploadprogress) {        //Progress          nslog (@ "Progress%@", uploadprogress);              } success:^ (nsurlsessiondatatask * Task, id responseobject)  {         nsdictionary  *dic = [nsjsonserialization jsonobjectwithdata:responseobject options: nsjsonreadingmutablecontainers error:nil];          nsarray *keys= [dic allkeys];                  //Traverse keys           for (int i=0;i<[keys count];i++)       & nbsp;  {             nsstring *ke Y=keys[i];              if ([key isequaltostring:@ "Data"])              {     &nbsp ;           NSString *url=[dic objectForKey:key];                  Url=[nsstri ng stringwithformat:@ "http://123.207.238.220:81%@", url];                  NSLog (@ "%@", URL);                  [self share:url];             }           }       
} failure:^ (Nsurlsessiondatatask *task, Nserror *error) {NSLog (@ "%@", [error description]);         NSLog (@ "%@", error); Because there is no valid address, so it must be the upload failed, in order to show the effect, here is also replaced with a new avatar}]; }

Ios--afnetworking uploading images to the server

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.