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"]) {   ; 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