如何在Iphone sdk中使用UIImagePickerViewController拍摄视频的自定义预览?

时间:2010-09-24 13:41:15

标签: objective-c

我需要自定义使用UIImagePickerController拍摄的视频的预览。

Guy Iam正在努力解决这个问题。请任何人帮助我摆脱这个问题。

我写的守则:

- (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");
}

}

任何人的帮助都会让我非常感激。

提前致谢。

纳尼。

1 个答案:

答案 0 :(得分:0)

我认为你不能通过界面做到这一点。我记得在一段时间后读了一个关于这个的问题,解决方案是创建自己的按钮来开始捕获。按下该按钮后,您开始记录时间并开始捕获。按下停止按钮时,您可以从currentTime和startCaptureTime计算视频的长度。

我可能在这里完全错了,但我会再搜索其他帖子。

- 更新 -

我认为这是this post,但也认为有一个例子。 :(