title: Most memory efficient way to save a photo to disk on iPhone?
body: <p>From profiling with <code>Instruments</code> I have learned that the way I am saving images to disk is resulting in memory spikes to <code>~60MB</code>. This results in the App emitting <code>low memory warnings</code>, which (inconsistently) leads crashes on the <code>iPhone4S</code> running <code>iOS7</code>.</p>
  
  <p>I need the most efficient way to save an image to disk.</p>
  
  <p>I am currently using this code </p>
  
  <pre><code>+ (void)saveImage:(UIImage *)image withName:(NSString *)name {
      NSData *data = UIImageJPEGRepresentation(image, 1.0);
      DLog(@"*** SIZE *** : Saving file of size %lu", (unsigned long)[data length]);
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
      [fileManager createFileAtPath:fullPath contents:data attributes:nil];
  }
  </code></pre>
  
  <p><b>Notes:</b></p>
  
  <ol>
  <li><p>Reducing the value of the <code>compressionQuality</code> argument in <code>UIImageJPEGRepresentation</code> does not reduce the memory spike significantly enough.
  e.g.
  <code>compressionQuality = 0.8</code>, reduced the memory spike by <code>3MB</code> on average over <code>100</code> writes.
  However, it does reduce the size of the data on disk (obviously)but this does not help me.</p></li>
  <li><p><code>UIImagePNGRepresentation</code> in place of <code>UIImageJPEGRepresentation</code> is worse for this. It is slower and results in higher spikes. </p></li>
  </ol>
  
  <p>Is it possible that <a href="https://gist.github.com/Cocoanetics/1845108" rel="noreferrer">this approach</a> with <code>ImageIO</code> would be more efficient? If so why?</p>
  
  <p>If anyone has any suggestions it would be great. Thanks</p>
  
  <p><b>Edit:</b></p>
  
  <p>Notes on some of the points outlined in the questions below.</p>
  
  <p>a) Although I was saving multiple images, I was not saving them in a loop. I did a bit of reading around and testing and found that an autorelease pool wouldn't help me.</p>
  
  <p>b) The photos were not 60Mb in size each. They were photos taken on the iPhone 4S.</p>
  
  <p>With this in mind I went back to trying to overcome what I thought the problem was; the line <code>NSData *data = UIImageJPEGRepresentation(image, 1.0);</code>.</p>
  
  <p>The memory spikes that were causing the crash can be seen in the screenshot below. They corresponded to when <code>UIImageJPEGRepresentation</code> was called. I also ran <code>Time Profiler</code> and <code>System Usage</code> which pointed me in the same direction.</p>
  
  <p><img src="https://i.stack.imgur.com/ScTO2.png" alt="enter image description here"></p>
  
  <p>Long story short, I moved over to <code>AVFoundation</code> and took the photo image data using</p>
  
  <p><code>photoData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];</code></p>
  
  <p>Which returns an object of type <code>NSData</code>, I then used this as the data to write using  NSFileManager.</p>
  
  <p>This removes the spikes in memory completely.</p>
  
  <p><b>i.e </b></p>
  
  <pre><code>[self saveImageWithData:photoData];
  </code></pre>
  
  <p>where</p>
  
  <pre><code>+ (void)saveImageWithData:(NSData *)imageData withName:(NSString *)name {
      NSData *data = imageData;
      DLog(@"*** SIZE *** : Saving file of size %lu", (unsigned long)[data length]);
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
      [fileManager createFileAtPath:fullPath contents:data attributes:nil];
  }
  </code></pre>
  
  <p>PS: I have not put this as an answer to the question incase people feel it does not answer the Title "Most memory efficient way to save a photo to disk on iPhone?". However, if the consensus is that it should be I can update it.</p>
  
  <p>Thanks.</p>