Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-29-2010, 07:46 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 8
vatsa.devil is on a distinguished road
Question Calling a method after view is Popped or Dismissed

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.
vatsa.devil is offline   Reply With Quote
Old 11-29-2010, 09:57 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

Try to send it on the viewDidAppear method, and set a UIActivityView or something like that on the viewWillAppear method, because the UI will still be blocked while you send it.
nobre84 is offline   Reply With Quote
Old 11-29-2010, 10:12 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vatsa.devil View Post
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.
You have a couple of issues.

First, and most importantly, you're using synchronous calls to upload your image to the server. That will cause your UI to freeze until the upload is complete. Your NSURLConnection sendSynchronousRequest: call is the culprit. You should rewrite that code to submit an async request. That way, the upload will take place in the background. I'd suggest doing a search on the web for asynchronous NSURLConnection. It will involve setting yourself up as a delegate and handling a few delegate methods that notify you about the progress of the upload.

If you want to take the easy route, and just pop your view before you begin a synchronous request, just rewrite your code that calls uploadPicToServer to let the pop take place first:


Code:
  [self uploadPicToServer:lastCapturedBg];
becomes


Code:
  [self performSelector: @selector(uploadPicToServer:) 
    withObject: lastCapturedBg
    afterDelay: 0];
What that does is to queue the call to uploadPicToServer: for later. Next time through the app's event loop, it will switch view controllers, and THEN do your call to uploadPicToServer:
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
camera, image, server

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 375
7 members and 368 guests
daudrizek, HemiMG, Kirkout, MarkC, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:48 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0