Saving image on iOS 5.1.1 works randomly
<p>I am trying to save an image taken from UIImagePickerController using the following code:</p>

<pre><code>NSString *docsDirectory = 
  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
    objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"myImage.png"];

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
</code></pre>

<p>After saving the image i again read the image in another screen :</p>

<pre><code>NSString *docsDirectory = 
  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
    objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"myImage.png"];
UIImage *extractedImage= [UIImage imageWithContentsOfFile:pngPath];
</code></pre>

<p>This code runs fine on iPhone 4 (iOS 5.1.1), but on devices iPhone 4's (iOS 5.1.1), iPad 1 (iOS5.1) and iPad 2's (iOS5.1.1) this code works randomly.</p>

<p>Sometimes the is image is saved the very first time and sometimes it gets saved after trying 2-3 times.</p>
