title: saving and reading array of images with its data in NSUserDefaults or in Document Directory
body: <p>I am trying to append image with its data in Array after downloading and then try to save in NSUserDefaults. But getting an error. I dont know what is proper way to save and read it .
  Can anyone please tell me how i can do this?
  Thanks</p>
  
  <pre><code>var imgIndex = 0
  var imageArray : [UIImage] = []
  typealias CompletionHandler = (image: UIImage) -&gt; Void
  
  downloadFileFromURL(NSURL(string: self.posts.objectAtIndex(indexPath.row).valueForKey("enclosure") as! String)!, completionHandler:{(img) in
                  dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                      cell.sideImageView.image = img
                      imageArray.insert(img, atIndex: self.imgIndex) //.append(img)
                      self.imgIndex++
                      print("Image append with data")
                      self.newsDefaults.setObject(imageArray, forKey: "image")
  
                  })
              })
  
  
  func downloadFileFromURL(url1: NSURL?,completionHandler: CompletionHandler) {
      // download code.
      if let url = url1{
          let priority = DISPATCH_QUEUE_PRIORITY_HIGH
          dispatch_async(dispatch_get_global_queue(priority, 0)) {
              let data = NSData(contentsOfURL: url)
              if data != nil {
                  print("image downloaded")
                  completionHandler(image: UIImage(data: data!)!)
              }
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/kemxK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kemxK.png" alt="enter image description here"></a></p>