Adding a upload indicator similar to flickr and instagram

iOS Simulator Screen shot Jun 20, 2013 5.55.45 PM

You can check out the code here:
https://github.com/kmdarshan/PhotoUpload.git

-(void) uploadPhotoToServer:(NSString*) path {
AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.kmdarshan.com"]];
NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/videograms/upload.php" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
{
UIImage *image = [UIImage imageNamed:path];
NSData *photoData = UIImageJPEGRepresentation(image, 1.0);
[formData appendPartWithFileData:photoData name:@"file" fileName:path mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSDecimalNumber *hundredNumber = [[NSDecimalNumber alloc] initWithInt:100];
NSDecimalNumber *dnumber = [[NSDecimalNumber alloc] initWithUnsignedLong:totalBytesExpectedToWrite];
NSDecimalNumber *vnumber = [[NSDecimalNumber alloc] initWithUnsignedLong:totalBytesWritten];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundUp
scale:0
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
NSDecimalNumber *finalnumber = [[vnumber decimalNumberByDividingBy:dnumber] decimalNumberByMultiplyingBy:hundredNumber withBehavior:roundUp];
[UIView animateWithDuration:1.0f animations:^{
int percentProgressBar = blockview.frame.size.width * [finalnumber intValue] / 100;
[progressBar setFrame:CGRectMake(0, 0, percentProgressBar, progressBar.frame.size.height)];
}];
}];
[httpClient enqueueHTTPRequestOperation:operation];
}

One response

Leave a Reply

Your email address will not be published. Required fields are marked *