如何在不按下图像选择器控制器上的照相按钮的情况下自动拍照?

问题描述 投票:3回答:3

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

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

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

}
objective-c xcode api uiimagepickercontroller
3个回答
6
投票

您可以调用- (void)takePicture;的方法- (void)takePicture;以编程方式拍摄照片。例如,您可以使用计时器每隔一分钟调用一次。

编辑

您应该首先显示相机界面(更多信息UIImagePickerController)。您可以在方法here中执行此操作。您还应该保留对创建的showCameraUI的引用。

UIImagePickerController

1
投票

最后我想出了解决方案。

我使用

- (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

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


0
投票

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

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer   
   fromConnection:(AVCaptureConnection *)connection 
© www.soinside.com 2019 - 2024. All rights reserved.