How to automatically take a picture without pressing the phototaken button on the imagepickercontroller?

和自甴很熟 提交于 2019-12-01 05:38:53

问题


In my project, I need to take pictures automatically every one minute. But I can't find out any solutions.

This is the code which I implemented but it doesn't work...

I used a NSTimer to call up the camera to take pictures every 4 sec. And I only need the 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:


You can call the method - (void)takePicture; of UIImagePickerController to take a picture programmatically. You can call it every one minute using a timer for example.

Edit

You should display the camera interface first (more info here). You can do this in the method showCameraUI. You should also keep a reference to the created 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];
    // ...
}



回答2:


Finally I figured out the solution.

I use the

 AVCaptureVideoDataOutputSampleBufferDelegate

to take pictures automatically.

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

It is pretty simple and thank you @sch all the same for your help :)




回答3:


Maybe a little late but the piker function has a property 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];
});


来源:https://stackoverflow.com/questions/9362785/how-to-automatically-take-a-picture-without-pressing-the-phototaken-button-on-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!