title: iOS AVFoundation captureStillImageAsynchronouslyFromConnection:completionHandler: Hanging Indefinitely
body: <p>I've implemented a camera using the AVFoundation framework provided in iOS 4 &amp; 5, but have been experiencing an inconsistent issue with the <code>captureStillImageAsynchronouslyFromConnection:completionHandler:</code> function where the completion handler block is never called. The shutter sound does not fire and the preview freezes in this situation.</p>
  
  <p>I've followed both Apple (WWDC 2010 &amp; 2011 videos) and non-Apple guides (blog and SO posts) to implement a still image capture at Photo resolution with little to no improvement. I've been able to reproduce somewhat consistently under the <strong>following conditions</strong>:</p>
  
  <ul>
  <li>If I set flash to <code>AVCaptureFlashModeAuto</code> and take a photo in low light, it hangs and never runs the completion block. The flash fires, but the shutter sound does not. With the same code I am able to take a photo at normal light where the shutter sound fires, flash does not, and completion block runs.</li>
  <li>If I do not set the <code>flashMode</code> or set it to off, I am able to take some photos, but after a small number (between 1 and 5, usually) the completion block stops firing. For clarification, a scenario might be:
  
  <ol>
  <li>Take a photo and view at confirmation screen</li>
  <li>Go back to take photo screen</li>
  <li>Repeat 1 &amp; 2 a couple times</li>
  <li>Hit take a photo and it hangs before the completion block.</li>
  </ol></li>
  </ul>
  
  <p><strong>My Implementation</strong></p>
  
  <p>I have the interface handled in a ViewController and separate class to handle all the AVFoundation stuff. An instance variable of the separate class that handles the AVFoundation stuff is in the ViewController. It's different than example implementations, but I don't see why it should cause this kind of occasional bug.</p>
  
  <p><strong>More Observations</strong></p>
  
  <ul>
  <li>Even when checking <code>[stillImageOutput isCapturingStillImage]</code>, it will continue to return false and continue to run the <code>captureStillImageAsynchronouslyFromConnection</code> function</li>
  <li>If I switch to another application, lock the phone, take certain other actions in the application, or tap the 'Take Photo' button a million times I am able to get the completion block to return an error. They are different based on which action. Common ones are 11800, 11801, and 11830. They seem to be unrelated to the hang.</li>
  <li>I've let our test phone (iPhone 4 GSM) sit for an hour to see if it would eventually process. No dice.</li>
  </ul>
  
  <p>I would love to know if there's something that I could be missing, a property left unset, or a known issue with a work around.</p>
  
  <p>Thanks.</p>
  
  <p>(I saw a similar post, but none of the answers solved my problem. I need Photo res in the application. For reference:
  <a href="https://stackoverflow.com/questions/3847140/iphone-sdk-4-avfoundation-how-to-use-capturestillimageasynchronouslyfromconnect">iPhone SDK 4 AVFoundation - How to use captureStillImageAsynchronouslyFromConnection correctly?</a>)</p>