8

I am using AVFoundation to display a Video in my UIView via an AVCaptureVideoPreviewOverlay. I then use AVStillImageOutput's -captureStillImageAsynchronouslyFromConnection: to capture a still Image from the Video with the AVCaptureSessionPresetPhoto preset.

I am freezing the video using AVCaptureSession's -stopRunning in the -captureStillImageAsynchronouslyFromConnection completion block mentioned earlier. However, it's too late and the video has continued running while the still image is taken, so the freeze is a second or two later. When I display the image there is a jump.

How can I freeze the video at the exact moment the photo is taken?

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
user1130254
  • 155
  • 2
  • 9
  • Do you perform `-stopRunning` as the very first action in your completion block? Other operations in that block might take a significant amount of time, so you'll want that to be the very first thing that happens in there. – Brad Larson Jul 27 '12 at 15:03
  • stopRunning is the very first action – user1130254 Jul 27 '12 at 16:50
  • Are you capturing your video frames on the main thread or a background thread? – Brad Larson Jul 27 '12 at 16:51
  • both the call to captureStillImage... and the stopRunning are explicitly told to be done on the main thread using performSelectorOnMainThread – user1130254 Jul 27 '12 at 20:11
  • Is your video itself being captured on the main thread? I'm asking because if the two were on different threads, your video could still be displayed for a few frames on that thread while the other is getting the asynchronous photo callback. If both are on the same thread, I've seen the latter block the former from updating video frames. – Brad Larson Jul 27 '12 at 20:45
  • Have you tried detaching the capture session from the preview? e.g. `previewLayer.session = nil` – Steve McFarlin Jul 31 '12 at 17:43
  • Did you ever get this figured out? I'm running into the same issue. – bmueller Oct 17 '12 at 03:03
  • Steve, where would you place the `previewLayer.session = nil` code? I tried placing it right outside the `-captureStillImageAsynchronouslyFromConnection:` block, but this causes the app to freeze for 2-3 seconds. – bmueller Oct 17 '12 at 03:13
  • And when I put it within the block, the preview disappears for ~1 second before the captured image appears... – bmueller Oct 17 '12 at 03:36

1 Answers1

1

Almost a year later...Your approach is all wrong. Instead of trying to pause the video at the precise moment that the image is captured why don't you pause the video and then capture that paused image. To a user it makes no difference, to a developer you don't have to worry about exact precision.

To reiterate my idea, if you pause a video and flash white visual and play a click the user will think you have just captured that frame regardless if you are or not. Actually, you could consider pausing video the same as capturing an image without saving it.

Gallonallen
  • 576
  • 5
  • 14