title: Error: The file...doesn't exist when calling writeToFile on imageData
body: <p>I am trying to write data to a file with the following code in a completion block for <code>NSURLSessionDownloadTask</code>:</p>
  
  <pre><code>   void (^completionBlock)(NSURLResponse *response, NSURL *filePath, NSError *error) = ^void (NSURLResponse *response, NSURL *filePath, NSError *error) {
          dispatch_async(dispatch_get_main_queue(), ^{
              if (!error){
                  NSData *imageData = [NSData dataWithContentsOfURL:filePath];
                  if(imageData) NSLog(@"image is not null");
  
                  if(pic == 1) self.imageView.image = [UIImage imageWithData:imageData];
                  else if(pic==2) self.imageView2.image = [UIImage imageWithData:imageData];
  
                  NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
                  NSURL *documentsDirectoryURL = [paths lastObject];
                  NSURL *saveLocation;
                  if(pic == 1) saveLocation = [documentsDirectoryURL URLByAppendingPathComponent:self.pictureName1];
                  else if (pic == 2) saveLocation = [documentsDirectoryURL URLByAppendingPathComponent:self.pictureName2];
                  else saveLocation = [documentsDirectoryURL URLByAppendingPathComponent:self.pictureName3];
  
                  NSLog(@"for # %d writing to file %@", pic, saveLocation);
  
                  NSError *error = nil;                
                  [imageData writeToFile:[saveLocation absoluteString] options:NSAtomicWrite error: &amp;error];
                  if(error){
                      NSLog(@"FAILED\n\n\n %@ \n\n\n", [error description]);
                  }
       }
  </code></pre>
  
  <p>I am able to display the downloaded images in <code>UIImageViews</code> and my null check on <code>imageData</code> likewise confirms it's not null. However, when I try to write the data to a file, My <code>NSLog</code> prints out the following error indicating that the write failed:</p>
  
  <pre><code>(log statements)
  # 3 writing to file file:///var/mobile/Containers/Data/Application/3743A163-7EE1-4A5A-BF81-7D1344D6DA45/Documents/pic3.png
  Error Domain=NSCocoaErrorDomain Code=4 "The file “pic1.jpg” doesn’t exist." 
  UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/3743A163-7EE1-
  4A5A-BF81-7D1344D6DA45/Documents/pic1.jpg, NSUnderlyingError=0x16d67200 {Error
  Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} 
  </code></pre>
  
  <p>I haven't been able to find another question on SO indicating this error message for this file, and I'm finding the error message quite counterintuitive. Where is my error?</p>