title: "The operation couldn’t be completed. (Cocoa error 512.)"
body: <p>I have this code, which should be working perfectly, but I can't udnerstand why it isn't:</p>
  
  <pre><code>+(NSString *)writeImageToFile:(UIImage *)image {
  
      NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f);
  
  
      NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images/"];
  
      NSFileManager *fileManager = [NSFileManager defaultManager];
      BOOL isDirectory = NO;
      BOOL directoryExists = [fileManager fileExistsAtPath:path isDirectory:&amp;isDirectory];
      if (directoryExists) {
          NSLog(@"isDirectory: %d", isDirectory);
      } else {
          NSError *error = nil;
          BOOL success = [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&amp;error];
          if (!success) {
              NSLog(@"Failed to create directory with error: %@", [error description]);
          }
      }
  
      NSString *name = [NSString stringWithFormat:@"%@.jpg", [JEntry generateUuidString]];
      NSString *filePath = [path stringByAppendingPathComponent:name];
      NSError *error = nil;
      BOOL success = [fullImageData writeToFile:filePath options:NSDataWritingAtomic error:&amp;error];
      if (!success) {
          NSLog(@"Failed to write to file with error: %@", [error description]);
      }
  
      return filePath;
  }
  </code></pre>
  
  <p>It passed the directoryExists without an error, but when it gets to writeToFile, it gives me this error:</p>
  
  <pre><code>Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x5634ee0 {NSFilePath=/var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/18DAE0BD-6CB4-4244-8ED1-9031393F6DAC.jpg, NSUnderlyingError=0x5625010 "The operation couldn’t be completed. Not a directory"}
  </code></pre>
  
  <p>Any ideas why this might be?</p>