objective c - Upload image with post -ios -
i use method save user informations in data base. can me upload image in same function ?
- (void)inscription:(nsarray *)value completion:(void (^)( nsstring * retour))block{ nsoperationqueue *queue = [[nsoperationqueue alloc] init]; nsarray *param_header = @[@"username", @"password", @"email",@"first_name", @"last_name"]; // nsarray *param_value = @[@"ali", @"aliiiiiiii", @"ali.ali@gmail.com",@"ali",@"zzzzz"]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:[nsurl urlwithstring:[nsstring stringwithformat:@"http:// /messenger/services/messenger_register"]]]; nsstring *aa=[self buildparameterwithposttype:@"user" andparameterheaders:param_header ansparametervalues:value]; [request sethttpbody:[aa datausingencoding:nsutf8stringencoding]]; [request sethttpmethod:@"post"]; [nsurlconnection sendasynchronousrequest: request queue: queue completionhandler: ^(nsurlresponse *response, nsdata *data, nserror *error) { if (error || !data) { // handle error nslog(@"server error : %@", error); } else { nserror *error = nil; id jsonobjects = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers error:nil]; block([nsstring stringwithformat:@"%@", [jsonobjects objectforkey:@"message"]]); } } ]; }
you can use third-party frameworks afn upload image。 here sample code:
afhttprequestoperationmanager *mgr = [afhttprequestoperationmanager manager]; nsmutabledictionary *params = [nsmutabledictionary dictionary]; params[@"username"] = @"jack"; [mgr post:@"http:// /messenger/services/messenger_register" parameters:params constructingbodywithblock:^(id<afmultipartformdata> formdata) { nsstring *file = [[nsbundle mainbundle] pathforresource:@"image.png" oftype:nil]; nsdata *data = [nsdata datawithcontentsoffile:file]; [formdata appendpartwithfiledata:data name:@"file" filename:@"image.png" mimetype:@"mage/png"]; } success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"upload success!----%@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"upload failed!----%@", error); }];
Comments
Post a Comment