tags:

views:

54

answers:

1

Hi Guys,

I need to customize the preview of captured video taken with UIImagePickerController.

Guy Iam struggling about this.Please any one help me to get out of this problem.

The Code I written:

- (BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject  
{  
printf("\n Inside StartCameraPickerFromViewController.................");
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    picker = [[[UIImagePickerController alloc] init]autorelease];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
    picker.delegate = self;
    picker.showsCameraControls=NO; 
    picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
    picker.allowsEditing = YES;
    picker.toolbarHidden = YES;

    overlayView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    CGRect imageFrame = CGRectMake(135, 415, 40, 40);   
    startRecording= [[MyCustomButton alloc] initWithIndexPath:nil];
    [startRecording setBackgroundImage: [UIImage imageNamed:@"recordbtn.png"] forState:UIControlStateNormal];
    [startRecording setFrame:imageFrame];
    [startRecording addTarget:self  action:@selector(startRecordingAction)  forControlEvents:UIControlEventTouchUpInside];
    [overlayView addSubview:startRecording];

    CGRect imageFrame1 = CGRectMake(50, 415, 40, 40);   
    stopRecording= [[MyCustomButton alloc] initWithIndexPath:nil];
    [stopRecording setBackgroundImage: [UIImage imageNamed:@"stopbtn.png"] forState:UIControlStateNormal];
    [stopRecording setFrame:imageFrame1];
    [stopRecording addTarget:self  action:@selector(stopRecording)  forControlEvents:UIControlEventTouchUpInside];
    [overlayView addSubview:stopRecording];

    CGRect imageFrame2 = CGRectMake(210, 415, 40, 40);  
    cameraModeBtn= [[MyCustomButton alloc] initWithIndexPath:nil];
    [cameraModeBtn setBackgroundImage: [UIImage imageNamed:@"clap.png"] forState:UIControlStateNormal];
    [cameraModeBtn setFrame:imageFrame2];
    [cameraModeBtn addTarget:self  action:@selector(changeCameraMode)  forControlEvents:UIControlEventTouchUpInside];
    [overlayView addSubview:cameraModeBtn];


    picker.cameraOverlayView = overlayView;
    [controller presentModalViewController:picker animated:YES];

}

return YES;  
}  
 -(void)startRecordingAction
 {
startRecording.enabled = NO;
stopRecording.enabled = YES;
BOOL recording = [picker startVideoCapture];
if(recording == YES)
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
    printf("\n Recording Started....!!");
}
 }
 -(void)stopRecording
{
startRecording.enabled = YES;
stopRecording.enabled = NO;
[picker stopVideoCapture];
[timer invalidate];
NSString *durationStr = [NSString stringWithFormat:@"%d",numOfSeconds];
aCustObj.audioDuration = durationStr;
timeValue = 0;
minsValue = 0;

}
 -(void)changeCameraMode
{
if(isCamera == YES)
{
    isCamera = NO;
    picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
else 
{
    isCamera = YES;
    picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}

}
- (void)updateCounter:(NSTimer *)theTimer
{
numOfSeconds++;
printf("\n number of seconds....%d",numOfSeconds);
timeValue++;

if (timeValue == 60)
{
    timeValue = 0;
    minsValue++;
}

//NSString* str1 = @"0:"
NSString* strVal = [NSString stringWithFormat:@"%d:%d",minsValue,timeValue];
UIFont *uiFont1 = [UIFont boldSystemFontOfSize:20];
[durationLabel setFont:uiFont1];
durationLabel.text = strVal;
durationLabel.textColor = [UIColor redColor];
durationLabel.numberOfLines = 1;
durationLabel.backgroundColor = [UIColor clearColor];
[overlayView addSubview:durationLabel];
}
 - (NSData *)generatePostDataForData:(NSData *)uploadData
 {

UIImage *theImage1 = [UIImage imageWithData:aCustObj.picture];
NSData *imageData = UIImageJPEGRepresentation(theImage1,0.9);
printf("\n length of imageData...%d",[imageData length]);


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddhhmmss"];
NSString *fromDateString=[dateFormatter stringFromDate:[NSDate date]];
// Generate the post header:
NSString *fileNameString=@"";
fileNameString=[fileNameString stringByAppendingString:fromDateString];
fileNameString = [fileNameString stringByAppendingString:@".3gp"];
printf("\n fileNameString....%s",[fileNameString UTF8String]);
NSString* finalPostString=[NSString stringWithFormat:@"--AaB03x\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n",[fileNameString UTF8String]];

NSString *post = [NSString stringWithCString:[finalPostString UTF8String] encoding:NSASCIIStringEncoding];
NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

// Generate the mutable data variable:
NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
[postData setData:postHeaderData];

// Add the video:
[postData appendData: uploadData];

// Add the closing boundry:
[postData appendData: [@"\r\n--AaB03x\r\n" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];

if([aCustObj.messageTitle length]>0)
{
    printf("\n messageTitle.......%s",[aCustObj.messageTitle UTF8String]);
    [postData appendData:[[NSString stringWithFormat:@"--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:aCustObj.messageTitle] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:@"\r\n--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
if([aCustObj.phoneNumber length]>0)
{
    printf("\n phoneNumber in video upload method...%s",[aCustObj.phoneNumber UTF8String]);
    [postData appendData:[[NSString stringWithFormat:@"--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"phoneNo\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:aCustObj.phoneNumber] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:@"\r\n--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

}
if([aCustObj.userName length]>0)
{
    printf("\n Username in video upload method...%s",[aCustObj.userName UTF8String]);
    [postData appendData:[[NSString stringWithFormat:@"--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:aCustObj.userName] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:@"\r\n--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

if([aCustObj.audioDuration length]>0)
{
    [postData appendData:[[NSString stringWithFormat:@"--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"duration\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:aCustObj.audioDuration] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:@"\r\n--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
if([aCustObj.imeiNumber length]>0)
{
    [postData appendData:[[NSString stringWithFormat:@"--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imei\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:aCustObj.imeiNumber] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:@"\r\n--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
if([aCustObj.latitude length]>0)
{
    printf("\n latitude value...%s",[aCustObj.latitude UTF8String]);
    [postData appendData:[[NSString stringWithFormat:@"--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"latitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:aCustObj.latitude] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:@"\r\n--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
if([aCustObj.longitude length]>0)
{
    [postData appendData:[[NSString stringWithFormat:@"--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"longitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:aCustObj.longitude] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:@"\r\n--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

if([imageData length]>0)
{
    printf("\n Image..");
    [postData appendData:[[NSString stringWithFormat:@"--AaB03x\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"picture\"; filename=\"test.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:imageData];
    [postData appendData:[[NSString stringWithFormat:@"\r\n--AaB03x--"] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
}

// Return the post data:
return postData;
 }

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
 {
timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(myMethod) userInfo:nil repeats:YES];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
printf("\n mediaType = %s",[mediaType UTF8String]);

if ([mediaType isEqualToString:@"public.movie"]) 
{
    NSLog(@"got a movie");
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
     NSData *webData = [NSData dataWithContentsOfURL:videoURL];
    webData1 = [webData copy];

    myAlertView = [[UIAlertView alloc] initWithTitle:@"Enter the Message title" message:@"................." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 80.0);
    [myAlertView setTransform:myTransform];
    [myAlertView addSubview:messageTitleTextField];
    [messageTitleTextField setFont:[UIFont boldSystemFontOfSize:14]];
    messageTitleTextField.returnKeyType = UIReturnKeyDone;
    messageTitleTextField.keyboardAppearance  = UIKeyboardAppearanceDefault;
    messageTitleTextField.keyboardType = UIKeyboardTypeDefault;
    messageTitleTextField.delegate = self;
    [myAlertView show];
    //[mview addSubview:myAlertView];

    UILabel *theTitle = [myAlertView valueForKey:@"_titleLabel"];
    [theTitle setTextColor:[UIColor orangeColor]];

    UILabel *theBody = [myAlertView valueForKey:@"_bodyTextLabel"];
    [theBody setTextColor:[UIColor whiteColor]];

    UIImage *theImage2 = [UIImage imageNamed:@"voicebg.png"];    
    theImage2 = [theImage2 stretchableImageWithLeftCapWidth:16 topCapHeight:16];
    CGSize theSize = [myAlertView frame].size;

    UIGraphicsBeginImageContext(theSize);    
    [theImage2 drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];    
    theImage2 = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    myAlertView.layer.contents = (id)[theImage2 CGImage];
    [myAlertView release];
    [timer invalidate];
    //[webData release];
    [picker dismissModalViewControllerAnimated:YES];

}   
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
printf("length of webdata...%d",[webData1 length]);
if(alertView == myAlertView)
{
    if (buttonIndex == 0) 
    {
        [self post:webData1];
    }
    else 
    {
        [self post:webData1];
    }
}

}

- (void)post:(NSData *)fileData
 {

NSLog(@"POSTING");

// Generate the postdata:
NSData *postData = [self generatePostDataForData: fileData];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

// Setup the request:
NSMutableURLRequest *uploadRequest = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://123.237.186.221:8080/uploadIphone/videoUpload.jsp"] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ] autorelease];
[uploadRequest setHTTPMethod:@"POST"];
[uploadRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];
[uploadRequest setHTTPBody:postData];

// Execute the request:
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self];
if (conn)
{
    // Connection succeeded (even if a 404 or other non-200 range was returned).
    NSLog(@"sucess");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got Server Response" message:@"Success" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
else
{
    // Connection failed (cannot reach server).
    NSLog(@"fail");
}

}

Anyone's help will me much appreciated.

Thanks in advance.

Nani.

A: 

I don't think you can do that through the interface. I remember reading a question about this some time back and the solution was to create your own button that starts the capture. When that button is pressed you start record the time and starts the capture. When they press your stop button you can calculate the length of the video from the currentTime and the startCaptureTime.

I might be completely wrong here, but I will do another search for that other post.

--update--

I think it was this post, but thought there was an example as well. :(

willcodejavaforfood
I had done the according to your suggestion.When I clicked on to the start recording I started the timer and when I tapped on to the stop button I calculated the duration.But the problem is when I tapped on to the stop button stopRecording method calls and picker dismisses.after stopping I need to preview the captured video and publish it to server.How can I make the customization of preview of captured video.
Monish
@Monish - OK that seems very different from your original question. Could you please update it with a thorough description of the problem since you are clearly not looking for the duration of the captured video? :)
willcodejavaforfood
Atually Iam customizing the UIImagePickerCOntroller.I am having 5 buttons 1)start recordind 2)stop recording 3)Preview 4)Front camera or rear Camera 5)Publish to server. Here when I tap start recording button I am calling the method startVideoCapture and I starts the timer for duration and When I tap to stop button I am calling the stopVideoCapture method and invalidated the timer and calculated the duration I also need to preview the captured video can you please suggest me how to do this.I will update the post with my code.
Monish
I had posted my code.Please suggest me how to customize the preview of video captured.Is there any property to do so?
Monish