如何在不按uiimagepickercontroller上的拍照按钮的情况下自动拍照?

时间:2012-02-20 14:32:42

标签: objective-c xcode api uiimagepickercontroller

在我的项目中,我需要每分钟自动拍照。但我找不到任何解决方案。

这是我实施的代码,但它不起作用......

我使用NSTimer调用相机每4秒拍照。而且我只需要这个东西

//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];

}

3 个答案:

答案 0 :(得分:5)

您可以调用UIImagePickerController的{​​{3}}方法以编程方式拍照。例如,您可以使用计时器每隔一分钟调用一次。

修改

您应首先显示相机界面(更多信息- (void)takePicture;)。您可以在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];
    // ...
}

答案 1 :(得分:1)

最后我找到了解决方案。

我使用

 AVCaptureVideoDataOutputSampleBufferDelegate

自动拍照。

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

这很简单,谢谢@sch同样的帮助:)

答案 2 :(得分:0)

也许有点晚,但是piker函数具有属性takePicture

double delayInSecondse = 3.0;
dispatch_time_t epopTime = dispatch_time(DISPATCH_TIME_NOW, delayInSecondse * NSEC_PER_SEC);
dispatch_after(epopTime, dispatch_get_main_queue(), ^(void){
  [picker takePicture];
});