I have a view with a button to launch the camera. and after taking the picture, i need to send it over to a server AND display the image in a UIImageView. everything works fine, except that, the function that is sending the pic to the server gets called before the camera view is dismissed. i.e. after taking the pic, i hit 'use' and the app just freezes for about 20 seconds (highly unacceptable) and then comes back to the original view. Where as what i want is the camera view should just pop and come back to the original view and ten send the data over. Can't figure out what i'm doing wrong.
Following are some relevant code.
//For launching the camera
Code:
-(void)takeAPicPressed:(id)sender{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
save the captured image in lastCapturedBg;
Code:
- (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
picUpdated=TRUE;
lastCapturedBg = [info objectForKey:UIImagePickerControllerOriginalImage];
[[Picker parentViewController] DismissModalViewControllerAnimated:NO];
}
viewWillAppear
Code:
-(void)viewWillAppear:(BOOL)animated{
NSLog(@"viw will appr");
bgImage.image=lastCapturedBg;
//just to make sure the method doesn't get called on the first run..
if (picUpdated) {
picUpdated=FALSE;
[self uploadPicToServer:lastCapturedBg];
}
}
sending file over to the server
Code:
-(void)uploadPicToServer:(UIImage*)myPic{
NSString *urlString = @"http://xxx.xxx.xx.xx/xxxxxxxxxxx/uploadFile";
NSString *filename = g_userEmailId;
NSURLRequest* request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"-----------------------------4664151417711";
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=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *myData= UIImagePNGRepresentation(myPic);
[postbody appendData:[NSData dataWithData:myData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",@"-----------------------------4664151417711",@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
}
putting breakpoints shows that the modelView has been dismissed before the method gets called, but the 'picker' view is still shown on the screen. Any help is appreciated. Thank you.