ios - error while trying to upload image with NSURLSession -
i'm using following code upload , image:
- (ibaction)uploadpic:(uibutton *)sender { phfetchresult *result = [phasset fetchassetswithalasseturls:@[self.imagereferenceurl] options:nil]; phasset *asset = [result firstobject]; if (asset) { phimagemanager *manager = [phimagemanager defaultmanager]; [manager requestimagedataforasset:asset options:nil resulthandler:^(nsdata *imagedata, nsstring *datauti, uiimageorientation orientation, nsdictionary *info) { // upload `imagedata` nsurl *fileurl = info[@"phimagefileurlkey"]; nsstring *filename = [fileurl lastpathcomponent]; nsstring *mimetype = [self mimetypeforpath:filename]; nsstring *urlstring = @"url"; nsmutableurlrequest* request= [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:urlstring]]; [request sethttpmethod:@"post"]; nsstring *boundary = @"---------------------------14737809831466499882746641449"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",boundary]; [request addvalue:contenttype forhttpheaderfield: @"content-type"]; nsmutabledata *postbody = [nsmutabledata data]; [postbody appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n",boundary] datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"uploaded_file\"; filename=\"%@\"\r\n", filename] datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[[nsstring stringwithformat:@"content-type: %@\r\n\r\n", mimetype] datausingencoding:nsutf8stringencoding]]; [postbody appenddata:imagedata]; [postbody appenddata:[[nsstring stringwithformat:@"\r\n--%@--\r\n",boundary] datausingencoding:nsutf8stringencoding]]; nsurlsessiontask *task = [[nsurlsession sharedsession] uploadtaskwithrequest:request fromdata:postbody completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nsstring *responsestring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; nslog(@"response %@",responsestring); }]; [task resume]; }]; } } - (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { uiimage *chosenimage = info[uiimagepickercontrollereditedimage]; self.imageview.image = chosenimage; self.imagereferenceurl = info[uiimagepickercontrollerreferenceurl]; [picker dismissviewcontrolleranimated:yes completion:nil]; }
and got error:
2015-09-30 10:35:42.921 udazz[874:453021] * terminating app due uncaught exception 'nsinvalidargumentexception', reason: '* -[__nsplaceholderarray initwithobjects:count:]: attempt insert nil object objects[0]' *** first throw call stack: (0x183804f5c 0x198307f80 0x1836ee30c 0x1836f9db4 0x100020240 0x188d923e4 0x188d92360 0x188d7ac88 0x188d91c78 0x188d918a8 0x188d8aadc 0x188d5ba2c 0x188d59f18 0x1837bc5a4 0x1837bc038 0x1837b9d38 0x1836e8dc0 0x18e83c088 0x188dc2f60 0x1000315a4 0x198b328b8) libc++abi.dylib: terminating uncaught exception of type nsexception
Comments
Post a Comment