0

I am attempting to create a application in xcode 8 swift 3 where the user adds an image using a imagePickerController then when the user clicks either save or exits the imagePickerController I want the image to be saved locally, so next time the app is loaded (for example after a device restart) the image is there. Just to be clear I Do Not want to save the image to the camera roll. Here is my code thus far without any save or load methods, (just the save button reference) any help would be very appreciated as I have been attempting to do this for a long while.

import UIKit


class timetable: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet var imageviewtimetable: UIImageView!


@IBAction func save(_ sender: Any) {


}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func selectImageFromPhotoLibrary(_ sender: Any) {

    let imagePickerController = UIImagePickerController()
    imagePickerController.sourceType = .photoLibrary
    imagePickerController.delegate = self
    present(imagePickerController, animated: true, completion: nil)


}



func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
    dismiss(animated: true, completion:nil)
}


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

    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    imageviewtimetable.image = selectedImage

    dismiss(animated: true,completion:nil)
}





}
Yellow
  • 175
  • 1
  • 15
  • You can save it to `NSUserDefaults`: http://stackoverflow.com/a/6648563/188331, though it's not a good method. Explanation & better solution [here](http://stackoverflow.com/a/32793289/188331) – Raptor Mar 21 '17 at 06:31

1 Answers1

1

For info : click here

Save image in Document Directory

    func saveImageDocumentDirectory() {
        let fileManager = NSFileManager.defaultManager()
        let paths = (getDirectoryPath() as NSString).stringByAppendingPathComponent("apple.jpg")
        let image = UIImage(named: "apple.jpg")
        print(paths)
        let imageData = UIImageJPEGRepresentation(image!, 0.5)
        fileManager.createFileAtPath(paths as String, contents: imageData, attributes: nil)
    }

Get Document Directory Path

    func getDirectoryPath() -> String {
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let documentsDirectory = paths[0]
        return documentsDirectory
    }

get Image from document directory

    func getImage(){
        let fileManager = NSFileManager.defaultManager()
        let imagePath = (self.getDirectoryPath() as NSString).stringByAppendingPathComponent("apple.jpg")
        if fileManager.fileExistsAtPath(imagePath){
            self.imageView.image = UIImage(contentsOfFile: imagePath)
        } else {
            print("No Image")
        }
    }

create Directory

    func createDirectory(){
        let fileManager = NSFileManager.defaultManager()
        let paths = getDirectoryPath() as NSString).stringByAppendingPathComponent("customDirectory")
        if !fileManager.fileExistsAtPath(paths){
            try! fileManager.createDirectoryAtPath(paths, withIntermediateDirectories: true, attributes: nil)
        } else {
            print("Already dictionary created.")
        }
    }
DoruChidean
  • 7,941
  • 1
  • 29
  • 33
nick
  • 114
  • 1
  • 4
  • 1
    Downvoted for copypasting from external URL without proper attribution to the creator. https://iosdevcenters.blogspot.com/2016/04/save-and-get-image-from-document.html Do not post code from third party websites without attribution and copyright information. Use the comment section and link the OP to the source instead. –  Mar 21 '17 at 06:40
  • is it fine? Mr. Sneak – nick Mar 21 '17 at 07:23
  • Yes :) I have upvoted you. Edit it and type "Taken from this source:" –  Mar 21 '17 at 07:28
  • I believe the issue I am encountering is that the image will not rely on itself, it is giving me the Thread 1_EXC BREAKPOINT any ideas? I believe this method may work best for what I am trying to achieve – Yellow Mar 21 '17 at 07:41
  • Going by the tags the User expected the code to be in Swift 3 – Md. Ibrahim Hassan Mar 21 '17 at 08:11
  • if I am to name a image in assets apple (i have changed it to PNGRepresentation) then it won't crash and i believe it will save however without this asset then it crashes – Yellow Mar 21 '17 at 09:03