2012-02-20 39 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

相关问题