0

How can I use NSDocument to store the images in Swift iOS programmatically?

    let alertController = UIAlertController(title: "Choose an option", message: "", preferredStyle: .Alert)

    let cameraRollAction = UIAlertAction(title: "Camera Roll", style: .Default) { (action) in
        self.imagePicker.delegate = self
        self.imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
        self.imagePicker.allowsEditing = true
        self.presentViewController(self.imagePicker, animated: true, completion: nil)

    }
    alertController.addAction(cameraRollAction)

    let takePictureAction = UIAlertAction(title: "Take a picture", style: .Default) { (action) in
        self.imagePicker.delegate = self
        self.imagePicker.allowsEditing = true
        self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        self.imagePicker.cameraCaptureMode = .Photo
        self.imagePicker.modalPresentationStyle = .FullScreen
        self.presentViewController(self.imagePicker,
            animated: true,
            completion: nil)
    }

I need to store the images when selecting the button and retrieve them when required.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Rakesh Mohan
  • 601
  • 2
  • 11
  • 23
  • see this link for [save](http://stackoverflow.com/questions/28066688/how-to-save-an-image-path-within-my-app) and [retrieve](http://stackoverflow.com/questions/29005381/get-image-from-documents-directory-swift) – Anbu.Karthik Jan 25 '16 at 12:09

1 Answers1

0

implement delegate of UIImagePickerControllerDelegate

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

let image = info[UIImagePickerControllerOriginalImage] as? UIImage
let imageData = UIImagePNGRepresentation(image!)
let dirPaths = NSSearchPathForDirectoriesInDomains(
    .DocumentDirectory, .UserDomainMask, true)[0] as NSString

  let fullPath = dirPaths.stringByAppendingPathComponent("yourNameImg.png")
  let result = imageData.writeToFile(fullPath, atomically: true)
    dismissViewControllerAnimated(true, completion: nil) 

}
Sahil
  • 9,096
  • 3
  • 25
  • 29