在我的项目中,我需要每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];
}
您可以调用- (void)takePicture;
的方法- (void)takePicture;
以编程方式拍摄照片。例如,您可以使用计时器每隔一分钟调用一次。
编辑
您应该首先显示相机界面(更多信息UIImagePickerController
)。您可以在方法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];
// ...
}
自动拍照。
AVCaptureVideoDataOutputSampleBufferDelegate
非常简单,非常感谢@sch的帮助:)
也许有点晚,但是piker函数具有属性takePicture
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection