title: Saving Image Path From Documents Directory in Core Data, Retrieving Only Partial Image. Swift
body: <p>I am working on creating an app with Swift that will save and retrieve images both using <code>CloudKit</code> and <code>Core Data</code>.  With CloudKit I will save the image as a <code>CKAsset</code> and with <code>Core Data</code> I save the image in the documents directory, and then store the path name in <code>Core Data</code>.  When my app starts loading it will first look for any items stored locally, then check the cloud if no local data is stored (saves a lot of loading time).  Anyways, when I load the images from <code>CloudKit</code> they are fine, and the same goes for <code>Core Data</code> and the documents directory path <em>most of the time</em>. I've been noticing this problem periodically when images are loaded in <code>Core Data</code> using their path name, essentially the image comes back only partially, some of it is grayed out as if it stopped drawing 85% of the way through or something.  It's hard to find a good example because it doesn't happen all the time, but here is the best one I could find of my cat:
  <a href="https://i.stack.imgur.com/44JXb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/44JXb.png" alt="Partial Cat Image"></a></p>
  
  <p>See how approximately 15% of the image at the top is just grey?  It's never localized to any specific part of an image, happens on the top, left, right, bottom, even in the middle.  And like I said it is not at all a chronic issue. Here is my code used to store the image in the documents directory, and save the path name in <code>Core Data</code>:</p>
  
  <pre><code>// Make the unique path for the Image Name and save it
  let userImageName:String = "savedUserImage\(indexForImageName).jpg"
  let imagePath = saveInDocumentsDirectory(userImageName)
  
  // Save the game's reference image in the documents directory
  self.saveImage(user.referenceImage, path: imagePath)
  
  func saveImage (image: UIImage, path: String ) {
  
      let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
      jpgImageData?.writeToFile(path, atomically: true)
  
  }
  
  func documentDirectoryURL() -&gt; NSURL {
      let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
      return documentsURL
  }
  
  func saveInDocumentsDirectory(filename: String) -&gt; String {
  
      let fileURL = documentDirectoryURL().URLByAppendingPathComponent(filename)
      return fileURL.path!
  
  }
  </code></pre>
  
  <p>Then later upon loading the app, here is the code I use to retrieve my image path names, and then fetch the image from the documents directory:</p>
  
  <pre><code>if let image = newUser.imageName {
      let imagePath = saveInDocumentsDirectory(image)
  
      if let loadedImage = loadImageFromPath(imagePath) {
  
          user.referenceImage = loadedImage
  
      } else { self.customDelegate.alertUserErrorLoadingImage() }
  }
  
  func loadImageFromPath(path: String) -&gt; UIImage? {
  
      let image = UIImage(contentsOfFile: path)
  
      if image == nil {
  
          self.customDelegate.alertUserErrorLoadingImage()
      }
  
      return image
  
  }
  </code></pre>
  
  <p>If anybody has any good ideas about how to fix this problem, or even an idea as to why this is happening, I would greatly appreciate it.  My first thought is that since I'm using <code>CloudKit</code> and <code>Core Data</code>, that maybe at some point my app is saving an image that is in the process of being asynchronously loaded via <code>CloudKit</code>, and then getting interrupted somehow...but based on how I've programmed it that shouldn't ever happen.  I'm curious to see if this is a common problem, or if anyone else has experienced, and conquered it.  Thank you in advance!</p>