2010-09-24 15 views
1

我需要自定義使用UIImagePickerController拍攝的捕獲視頻的預覽。如何使用Iphone sdk中的UIImagePickerViewController捕獲視頻的自定義預覽?

蓋伊恩掙扎着這個,請任何人幫我擺脫這個問題。

代碼中,我寫道:

- (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 *[email protected]""; 
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"); 
} 

} 

任何人的幫助,我將非常感激。

在此先感謝。

Nani。

回答

0

我不認爲你可以通過界面做到這一點。我記得過了一段時間,我讀了一個關於這個問題的問題,解決方案是創建自己的按鈕來開始捕獲。按下該按鈕後,開始記錄時間並開始拍攝。當他們按下停止按鈕時,您可以通過currentTime和startCaptureTime來計算視頻的長度。

我可能在這裏完全錯了,但我會爲另一個帖子做另一個搜索。

--update--

我認爲這是this post,但想有一個例子也是如此。 :(

+0

我已經完成了根據您的建議。當我點擊開始錄製我開始計時器,當我點擊停止按鈕時,我計算了持續時間。但問題是當我點擊到停止按鈕停止錄製方法調用和選擇器關閉。停止後我需要預覽捕獲的視頻並將其發佈到服務器。如何定製捕獲的視頻的預覽。 – Monish 2010-09-24 13:53:03

+0

@Monish - 確定,這與您的原始問題看起來非常不同。你可以請更新一下這個問題的徹底描述,因爲你顯然不是在尋找被捕獲的視頻的持續時間嗎?:) – willcodejavaforfood 2010-09-24 13:58:37

+0

Iam自定義UIImagePickerCOntroller.I有5個按鈕1)開始記錄2)停止記錄3)預覽4)前置攝像頭或後置攝像頭5)發佈到服務器。在這裏,當我點擊開始錄製按鈕,我打電話的方法startVideoCapture,我啓動計時器的持續時間,當我點擊停止按鈕時,我打電話給stopVideoCapture方法,並使計時器無效並計算持續時間,我還需要預覽捕獲的視頻可以你請告訴我如何做到這一點。我將用我的代碼更新這篇文章。 – Monish 2010-09-24 14:06:50

相關問題