2012-08-16 29 views
1

我正在爲iPhone 4/4S製作密碼應用程序。當用戶輸入密碼失敗3次時,我想用iPhone的前置攝像頭拍攝用戶的電影。這不是什麼新鮮事,appstore中的很多應用程序都做類似的事情。拍攝這個傢伙的照片,獲取他的GEO座標等。使用UIImagePickerController祕密錄製電影

我面臨的挑戰是,當我嘗試設置電影錄製時,攝像頭覆蓋佔據了整個屏幕。我真正想要做的是讓用戶仍然看到登錄屏幕和按鈕,但祕密記錄和製作用戶的電影。有什麼辦法可以做到這一點?

這是我正在使用的代碼。

在我的* .h文件

@interface v1InstantController : UIViewController <UIImagePickerControllerDelegate> 
{ 
    UIImagePickerController *picpicker; 
} 

@property (nonatomic, retain) UIImagePickerController *picpicker; 

在我的* .m文件

-(IBAction) makeMovieNow 
{ 
    picpicker = [[UIImagePickerController alloc] init]; 
     picpicker.delegate = self; 


     picpicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     picpicker.sourceType = UIImagePickerControllerSourceTypeCamera; 

     picpicker.showsCameraControls = NO; 
     picpicker.navigationBarHidden = YES; 
     picpicker.wantsFullScreenLayout = NO; 

     picpicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie]; 
     picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo; 

     picpicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff; 
     picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront; 

     picpicker.videoQuality = UIImagePickerControllerQualityTypeHigh; 

     picpicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; 

     //The problem is right here! 
     //picpicker.cameraViewTransform = CGAffineTransformScale(picpicker.cameraViewTransform, 0.01, 0.01); 
     [self presentModalViewController:picpicker animated:YES]; 

     [picpicker startVideoCapture]; 
} 

的問題就在這裏 「presentModalViewController:picpicker」。當我使用它時,它會啓動帶有鳶尾花飛濺等的相機屏幕,並顯示整個屏幕上正在記錄的內容。即使我使用cameraViewTransform,它仍會禁用頁面上的任何內容,並將此相機覆蓋圖放在頁面中間。 (帶有一個非常小的相機覆蓋)我不希望用戶知道我正在錄製,並希望他認爲它像往常一樣生意。即讓他繼續嘗試在頁面上輸入密碼失敗。

回答

0

以下是對未來可能感興趣的任何人的答案。不要評分!

確保包括corevideo的和CoreMedia在您的* .m文件構架

在* .h文件中

@interface v1InstantController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate> 
{ 
    AVCaptureSession *session; 
    NSString *videoPath2; 
} 

@property (nonatomic, retain) AVCaptureSession *session; 
@property (nonatomic, retain) NSString *videoPath2; 

@synthesize session; 
@synthesize videoPath2; 

-(IBAction) makeMovieNow 
{ 

    NSLog(@"makeMovieNow .."); 

    //record movie 
    session = [[AVCaptureSession alloc] init]; 
    [session beginConfiguration]; 
    session.sessionPreset = AVCaptureSessionPresetHigh; 

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
    captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 


    NSError *error = nil; 
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    device = [self frontFacingCameraIfAvailable]; 
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 
    if (!videoInput) 
    { 
     // Handle the error appropriately. 
     NSLog(@"ERROR: trying to open camera: %@", error); 
    } 

    AVCaptureDevice *audioDevice  = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeAudio]; 
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error ]; 


    AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
    NSString *movieFileName = [NSString stringWithFormat: @"Secret.mov"]; 
    NSString *fullPathToFile2 = [documentsDirectoryPath stringByAppendingPathComponent:movieFileName]; 
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:fullPathToFile2]; 

    videoPath2=[outputURL path]; 


    [session addInput:videoInput]; 
    [session addInput:audioInput]; 
    [session addOutput:movieFileOutput]; 
    [session commitConfiguration]; 

    //start recording 
    [session startRunning]; 
    [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 
} 

-(IBAction) makeMovieStop 
{ 
    NSLog(@"makeMovieStop ..."); 

    //stop recording 
    [session stopRunning]; 

    //save video to photo-album 
    UISaveVideoAtPathToSavedPhotosAlbum(videoPath2, self, nil, nil); 

} 
5

UIImagePickerController提供了一種無需太多努力即可從用戶獲取照片/視頻的方式,因此用戶始終可以看到它。

既然你似乎需要更多的控制,你可以看看AVFoundation。 Apple Documentation應該提供一個很好的起點。

+0

你知道的任何工作示例/樣品碼? – 2012-08-16 14:07:44

+0

有幾個示例項目可用:[AVCam](https://developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010112)的基礎知識,[SquareCam](https://developer.apple.com/library/ios/#samplecode/SquareCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011190)也會很有趣。 – 2012-08-16 14:34:51