1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

using UIProgressView

Discussion in 'iPhone/iPad Programming' started by uaecasher, Nov 25, 2009.

  1. macrumors 65816



    I was wondering how can I implement the UIProgressView to show my users the status of their upload.

    here is my upload code:

    - (void)viewDidLoad {
    	self.imgPicker = [[UIImagePickerController alloc] init];
    	self.imgPicker.allowsImageEditing = YES;
    	self.imgPicker.delegate = self;		
    - (IBAction)grabImage {
    	[self presentModalViewController:self.imgPicker animated:YES];
    - (IBAction)uploadImage {
    	 turning the image into a NSData object
    	 getting the image back out of the UIImageView
    	 setting the quality to 90
    	NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
    	// setting up the URL to post to
    	NSString *urlString = @"http://mozymac.com/upload.php";
    	// setting up the request object now
    	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    	[request setURL:[NSURL URLWithString:urlString]];
    	[request setHTTPMethod:@"POST"];
    	 add some header info now
    	 we always need a boundary when we post a file
    	 also we need to set the content type
    	 You might want to generate a random boundary.. this is just the same 
    	 as my output from wireshark on a valid html post
    	NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    	NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    	 now lets create the body of the post
    	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]];
    	// setting the body of the post to the reqeust
    	[request setHTTPBody:body];
    	// now lets make the connection to the web
    	NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    	NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    	image.image = img;	
    	[[picker parentViewController] dismissModalViewControllerAnimated:YES];
    	// need to show the upload image button now
    	upload.hidden = NO;
  2. Moderator


    Staff Member

    Don't use sendSynchronousRequest:.

    Use the asynchronous methods with a delegate. It's well covered in the documentation and there are lots of examples to be found on here before you ask :)

Share This Page