My array is setup as follows:
var images = [UIImage]()
As i add images in the app to the array i would like to us NSFileManager to save and retrieve those files when opening the app again.
func saveImageDocumentDirectory(){ let fileManager = NSFileManager.defaultManager()
//Error below in Paths
let paths = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString).stringByAppendingPathComponent()
//Error below in setting the image constant
let image = UIImage(imageView.image)
print(paths)
let imageData = UIImageJPEGRepresentation(image!, 0.5) fileManager.createFileAtPath(paths as String, contents: imageData, attributes: nil) }
Also the code for inserting the image into the collection view is as follows below
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageView", for: indexPath)
if let imageView = cell.viewWithTag(1000) as? UIImageView { imageView.image = images[indexPath.item] }
return cell }
@objc func importPicture() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
present(picker, animated: true) }
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else { return } dismiss(animated: true)
images.insert(image, at: 0) collectionView?.reloadData() }