ios - Uploading Big Image In Background Thread. -


i need upload image webservice. below code snippet have return upload image. image size big (around 6mb). uploading image in background thread using gcd.

       if([vscore connectedtointernet ])        {          bgtask = [[uiapplication sharedapplication]beginbackgroundtaskwithexpirationhandler: ^{                             dispatch_async(dispatch_get_main_queue(), ^{                                 //[application endbackgroundtask:self->bgtask];                                 //self->bgtask = uibackgroundtaskinvalid;                             });                         }];                          dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{                              [vrs write:data touri:uri];                             [[uiapplication sharedapplication]endbackgroundtask:bgtask];                              bgtask = uibackgroundtaskinvalid;                         }); 

//
}

-(bool)write:(nsdata *)data touri:(nsstring *)uri { bool retval = no; nsstring* requestdatalengthstring = [[nsstring alloc] initwithformat:@"%d", [data length]];  nsrange range = [uri rangeofstring:@"http"];//is http?   if(range.location != nsnotfound)  {        //yes, http     nsmutableurlrequest *httprequest = [[nsmutableurlrequest alloc] initwithurl:[nsurl urlwithstring:uri]];      [httprequest sethttpmethod:@"post"];     [httprequest sethttpbody:data];     [httprequest setvalue:@"application/xml" forhttpheaderfield:@"content-type"];     [httprequest setvalue:requestdatalengthstring forhttpheaderfield:@"content-length"];      nsurlconnection *theconnection=[[nsurlconnection alloc] initwithrequest:httprequest delegate:self];      [theconnection release];     [httprequest release];      if (theconnection)      {         receiveddata=[[nsmutabledata data] retain];         retval = yes;     }      else      {                                                     nserror *error = [nserror alloc];         nslog(@"connection failed! error - %@ %@",             [error localizeddescription],             [[error userinfo] objectforkey:nsurlerrorfailingurlstringerrorkey]);             [error release];          retval = no;     }  }                                             return retval;   

}

now problem facing is, if try upload image in background thread request not going server ( checking log file on server). if upload image in main thread request going server (just testing purpose, know not idea upload big images in main thread). doing wrong here ? there problem background threading ? plz me out. in advance.

instead of doing on background thread. create class net connections this.

you'll need add in fields post image.

- (void)send: (nsstring *)urlstring {  self.receiveddata = [[nsmutabledata alloc] init];  nsurlrequest *request = [[nsurlrequest alloc]                          initwithurl: [nsurl urlwithstring:urlstring]                          cachepolicy: nsurlrequestreloadignoringlocalcachedata                          timeoutinterval: 20                          ];  nsurlconnection *connection = [[nsurlconnection alloc]                                initwithrequest:request                                delegate:self                                startimmediately:yes]; if(!connection) {     nslog(@"connection failed :("); } else {     nslog(@"connection succeeded  :)");  }  }   - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { //nslog(@"received response: %@", response);  [receiveddata setlength:0]; }  -(void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { //nslog(@"received %d bytes of data", [data length]);   [receiveddata appenddata:data]; //nslog(@"received data %d bytes", [receiveddata length]);   }  - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { nslog(@"error receiving response: %@", error); }  - (void)connectiondidfinishloading:(nsurlconnection *)connection { // once method invoked, "responsedata" contains complete result //nslog(@"succeeded! received %d bytes of data", [receiveddata length]);   nsstring *datastr=[[nsstring alloc] initwithdata:receiveddata encoding:nsasciistringencoding];  nslog(@"%@",datastr);  } 

you'll need in header:

@interface netconnection : nsobject  { nsmutabledata *receiveddata;  }  @property (nonatomic,retain) nsmutabledata *receiveddata; @property (nonatomic,retain) nsstring *callback; 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -