2012-02-20 67 views
3

在我的項目,我需要自動拍照每隔一分鐘。但我找不到任何解決方案。如何在不按下圖像選擇控制器上的攝影按鈕的情況下自動拍照?

這是我實現的代碼,但它不工作...

我使用的NSTimer調出攝像頭拍照,每4秒。而我只需要takepic

//This method is all for the time setup. You can ignore it. 

-(NSDate *)userInfo { 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm:ss"]; 

NSDate *date = [[[NSDate alloc]init]autorelease]; 

NSString *formattedDateString = [dateFormatter stringFromDate:date]; 

NSLog(@"formattedDateString: %@", formattedDateString); 

return date;  
} 


- (void)targetMethod:(NSTimer *)theTimer { 
    NSDate *startDate = [self userInfo]; 

    //newly changed lines. 
    UIImagePickerController *myPicker; 
    [myPicker takePicture]; 
    NSLog(@"Timer started on %@", startDate); 

} 


- (IBAction) showCameraUI { 


    [NSTimer scheduledTimerWithTimeInterval:4.0 
           target:self 
           selector: @selector(targetMethod:) 
           userInfo:[self userInfo] 
           repeats:YES]; 

} 

回答

1

最後我想出瞭解決方案。

我使用

AVCaptureVideoDataOutputSampleBufferDelegate 

自動拍照。

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
    fromConnection:(AVCaptureConnection *)connection 

這是很簡單的,感謝您@sch都是一樣的對你的幫助:)

5

您可以調用的方法UIImagePickerController- (void)takePicture;拍照程序。例如,您可以使用計時器每隔一分鐘調用一次。

編輯

你應該首先顯示相機的接口(更多信息here)。您可以在方法showCameraUI中執行此操作。您還應該保留對創建的UIImagePickerController的引用。

- (IBAction) showCameraUI 
{ 
    UIImagePickerController *picker; 
    // create and display picker 

    self.imagePicker = imagePicker; 
    [NSTimer scheduledTimerWithTimeInterval:4.0 
          target:self 
          selector: @selector(targetMethod) 
          userInfo:nil 
          repeats:YES]; 
} 

- (void)targetMethod 
{ 
    [self.picker takePicture]; 
    // ... 
} 
+0

感謝這麼多的幫助。但我無法讓它成功運行。你能讀我的臺詞並給我一些建議嗎?我是新的客觀-c,所以... – 2012-02-21 12:00:13

+0

你能描述你現在有什麼行爲嗎?是否執行了'targetMethod:'?你確定執行targetMethod時'picker'不是'nil'嗎? – sch 2012-02-21 12:22:47

+0

我相信targetMethod:能夠運行,因爲如果我實現了的UIImagePickerController來看,它可以定期出來。但是,如果我只實現 – 2012-02-21 15:19:37

相關問題