Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在不按下imagepickercontroller上的拍照按钮的情况下自动拍照?_Objective C_Xcode_Api_Uiimagepickercontroller - Fatal编程技术网

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

Objective c 如何在不按下imagepickercontroller上的拍照按钮的情况下自动拍照?,objective-c,xcode,api,uiimagepickercontroller,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 setDateF

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

这是我实现的代码,但它不工作

我用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];

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

编辑

您应该首先显示摄像头界面(更多信息)。您可以在方法
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];
    // ...
}

最后我想出了解决办法

我使用

 AVCaptureVideoDataOutputSampleBufferDelegate
自动拍照

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

它非常简单,感谢@sch的帮助:)

可能有点晚了,但是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];
});

非常感谢你的帮助。但我无法让它成功运行。你能读懂我的台词并给我一些建议吗?我是objective-c的新手,所以…你能描述一下你现在的行为吗?是否执行了
targetMethod:
?执行
targetMethod:
时,您确定
picker
不是
nil
吗?我确定targetMethod:可以运行,因为如果我实现UIImagePickerController视图,它可以定期显示。但是如果我只实现了简单的验证,那么在
[picker takePicture]行中放置一个断点
并查看调试器是否停止在那里。还要检查选择器是否不是
nil
。。。然后我更改了代码,但程序在[picker takePicture]接收到EXC_BAD_访问;