I am working on simple camera app which will take photo without any user interaction by using AV Foundation.. By using timer i have achieved that but now I want to enable auto flash for my back camera
i) Flash should Enable if i am in dark place
ii ) Flash should not enable if i am in sun light or under some light coverage area
- (void)StartTimer
{
seconds = 1;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(subtractTime) userInfo:nil repeats:YES];
}
- (void)subtractTime
{
seconds--;
// timerLabel.text = [NSString stringWithFormat:@"%02d",(int)seconds];
if(seconds == 0)
{
[timer invalidate];
session = [[AVCaptureSession alloc]init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];
AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
if ([session canAddInput:deviceInput])
{
[session addInput:deviceInput];
}
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view]layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.imageforcapture.frame;
[previewLayer setFrame:frame];
[rootLayer insertSublayer:previewLayer atIndex:0];
StillImageOutput = [[AVCaptureStillImageOutput alloc]init];
NSDictionary *outputSettings =[[NSDictionary alloc]initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
[StillImageOutput setOutputSettings:outputSettings];
[session addOutput:StillImageOutput];
[session startRunning];
// Timer to take picture
seconds = 1;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(takePicture) userInfo:nil repeats:YES];
}
}
-(void)takePicture
{
AVCaptureConnection *videoConnection = nil;
for(AVCaptureConnection *connection in StillImageOutput.connections)
{
for(AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo]){
videoConnection =connection;
break;
}
}
}
[StillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
if (imageDataSampleBuffer!=NULL)
{
NSData *imageData =[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
self.imageArray = [[NSMutableArray alloc]init];
self.image = [ UIImage imageWithData:imageData];
[self.imageArray insertObject:self.image atIndex:0];
self.image_view.image=[self.imageArray objectAtIndex:0];
}
}];
[timer invalidate];
seconds = 1;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(toSwitchFrontCamera) userInfo:nil repeats:YES];
}