2012-05-14 110 views
4

我想用UIImagePickerController錄製視頻。我的問題是,方法[imagePickerController startVideoCapture];總是返回0.我正在測試運行iOS 5.1的iPhone 4S的應用程序。可能有人請幫我這個:UIImagePickerController不會錄製視頻

- (IBAction)playButtonPushed:(id)sender 
{ 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    { 
     UIView *videoView = self.videoViewController.view; 
     UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] >init]; 
     imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
     imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects: (NSString >*)kUTTypeMovie, nil]; 
     imagePickerController.showsCameraControls = NO; 
     imagePickerController.toolbarHidden = NO; 
     imagePickerController.wantsFullScreenLayout = YES; 
     imagePickerController.allowsEditing = YES; 
     imagePickerController.videoQuality = UIImagePickerControllerQualityTypeMedium; 
     imagePickerController.videoMaximumDuration = 30; 
     [imagePickerController startVideoCapture]; 
     imagePickerController.cameraViewTransform = > CGAffineTransformScale(self.imagePickerViewController.cameraViewTransform, CAMERA_TRANSFORM, CAMERA_TRANSFORM); 
     imagePickerController.delegate = self; 

     [self.imagePickerViewController setCameraOverlayView:videoView]; 
     NSLog(@"%s videoCapture: %d", __PRETTY_FUNCTION__, [imagePickerController > startVideoCapture] 
      ); 
     [self presentModalViewController:imagePickerViewController animated:YES]; 
    } 
} 


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    NSLog(@"didFinishPickingMediaWithInfo"); 
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 
    NSData *videoData = [NSData dataWithContentsOfURL:videoURL]; 
} 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    NSLog(@"Cancel"); 
} 

回答

2

如果我不是錯,將「startVideoCapture」的方法是一個布爾

直接從蘋果的文檔摘自:

startVideoCapture

啓動視頻拍攝使用由UIImagePickerControllerCameraDevice屬性指定的相機。 (BOOL)startVideoCapture 返回值

是成功,否則失敗。結合

Movie capture is already in progress 

The device does not support movie capture 

The device is out of disk space 

討論

使用此方法與自定義重疊視圖啓動電影的拍攝計劃:這種方法可能會返回任何值因各種原因,其中包括以下。您可以在不離開界面的情況下拍攝多部電影,但要這樣做需要隱藏默認圖像選取器控件。

拍攝動畫時調用此方法不起作用。您必須調用stopVideoCapture方法,然後等待直到關聯的委託對象接收到imagePickerController:didFinishPickingMediaWithInfo:消息,然後才能捕獲其他影片。

當圖像選擇器的源類型設置爲UIImagePickerControllerSourceTypeCamera以外的值時,調用此方法將導致引發NSInvalidArgumentException異常。

如果您需要其他選項或更多控制動畫捕捉,請使用AV Foundation框架中的動畫捕捉方法。請參閱AV Foundation Framework Reference。 可用性

Available in iOS 4.0 and later. 

宣佈 UIImagePickerController.h stopVideoCapture

停止視頻拍攝。 - (無效)stopVideoCapture 討論

在調用此方法停止視頻採集,系統調用圖像拾取委託的imagePickerController:didFinishPickingMediaWithInfo:方法。 可用性

Available in iOS 4.0 and later. 

宣佈 UIImagePickerController.h

+0

不錯的複製和粘貼從文檔...... – nsij22

+0

是的,這是它對我來說。我認爲我的空間不足。 – sudo

+0

奇怪的是,它只是返回true/false而不是一些錯誤原因。 – sudo

1

認爲你需要調用presentModalViewController後致電startVideoCapture方法:動畫:而不是之前。

在屏幕上顯示UIImagePickerController並且動畫已停止後,您可能還需要很短的延遲(< 1秒?),然後調用startVideoCapture,否則有時候它不會開始記錄。

1

我有完全相同的問題。 AndyV沒有錯,直到選擇器出現後纔開始錄製。我花了很多時間試圖引進睡眠延遲,但最簡單的解決方法是使用顯示

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(startRecording) userInfo:nil repeats: NO]; 

然後選擇器後,啓動一個定時器..

- (void)startRecording 
{ 
    BOOL result = [picker startVideoCapture]; 
} 
1

我有同樣的問題而蘋果的文檔沒有幫助。這是因爲他們錯過一提的是startVideoCapture()可以返回false,如果您還沒有設置拍攝模式。視頻是這樣的:

picker.cameraCaptureMode = .video 

這爲我工作,我現在能夠捕獲視頻。

相關問題