0

I am showing preview to the user and after 3s, the image is taken.

    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
 [session stopRunning]; 
     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];

 }];

Because captureStillImageAsynchronouslyFromConnection takes some time, if i move phone just before 3s have passed, the picture taken will not be the same as the one shown in preview (AVCaptureVideoPreviewLayer).

Can i avoid this?

DixieFlatline
  • 7,895
  • 24
  • 95
  • 147

1 Answers1

0

You should try freezing/rendering your preview layer instead of stopping the AVCaptureSession.

You could try using this code to create a UIImage from the CALayer and just displaying the image in a UIImageView over your preview layer. I don't know if this rendering will be quick enough, but it's worth a try.

- (UIImage *)imageFromLayer:(CALayer *)layer
{
  UIGraphicsBeginImageContext([layer frame].size);

  [layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return outputImage;
}

This code is taken from:

UIImage from CALayer - iPhone SDK

Community
  • 1
  • 1
brynbodayle
  • 6,546
  • 2
  • 33
  • 49