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 07-08-2010, 04:06 PM   #1 (permalink)
New to the SDK
 
franzwarning's Avatar
 
Join Date: Jul 2010
Location: Portland, Oregon
Age: 19
Posts: 165
franzwarning is on a distinguished road
Send a message via AIM to franzwarning Send a message via Skype™ to franzwarning
Default Using UIActivityIndicatorView

Hey I have this code which uploads an image in the UIImageView to a server. Does anyone know how to add a UIActivityIndicatorView to it so it after I hit the upload button the activity indicator will pop up and will disappear before the last line [self dismissModalViewControllerAnimated:YES];

Cheers,
George

Code:
-(IBAction) uploadButtonPressed:(id) sender
{
	
	uploadLabel.hidden = FALSE;
	loadingSquare.hidden = FALSE;
	uploadIndicator.hidden = FALSE;
	[uploadIndicator startAnimating];
	
	
	NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
	NSString *urlString = @"http://defendthehouse.com/PhotoQuest/images/upload.php";
	
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
	[request setURL:[NSURL URLWithString:urlString]];
	[request setHTTPMethod:@"POST"];
	
	NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
	NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
	[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
	
	NSMutableData *body = [NSMutableData data];
	[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	[body appendData:[NSData dataWithData:imageData]];
	[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[request setHTTPBody:body];
	
	NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
	NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
	
	//	while(returnString != @"Temp file uploaded. \r\n"){
	
	
	[self dismissModalViewControllerAnimated:YES];	
	
	
	
	
}
franzwarning is offline   Reply With Quote
Old 07-08-2010, 04:15 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

The method you're using to upload the image is synchronous, so it runs on the main thread, blocking the UI.

You might want to look at an asynchronous equivalent, or run this upload code in a background thread.

If you choose the background thread option, be aware that UI can only be updated on the main thread.

An example of running on a background thread is as follows:
Code:
-(IBAction) uploadButtonPressed:(id) sender {
   uploadLabel.hidden = NO;
   loadingSquare.hidden = NO;
   uploadIndicator.hidden = NO;
   [uploadIndicator startAnimating];
   [NSThread detachNewThreadSelector:@selector(theProcess) toTarget:self withObject:nil];
}
- (void)theProcess {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   // do the upload
   [self performSelectorOnMainThread:@selector(processDone) withObject:nil waitUntilDone:NO];
   [pool release];
}
- (void)processDone {
   // stop the activityIndicator
   [self dismissModalViewControllerAnimated:YES];
}
harrytheshark is offline   Reply With Quote
Old 07-08-2010, 04:42 PM   #3 (permalink)
New to the SDK
 
franzwarning's Avatar
 
Join Date: Jul 2010
Location: Portland, Oregon
Age: 19
Posts: 165
franzwarning is on a distinguished road
Send a message via AIM to franzwarning Send a message via Skype™ to franzwarning
Default

Thank you so much, that worked like a charm!
franzwarning is offline   Reply With Quote
Old 07-08-2010, 04:42 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

No problem
harrytheshark is offline   Reply With Quote
Reply

Bookmarks

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: 338
13 members and 325 guests
akphyo, cgokey, EXOPTENDAELAX, GHuebner, guusleijsten, Mirotion22, ohmniac, Paul Slocum, PavelSea, Pudding, SLIC, Sloshmonster, Sonuye857
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

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