如何保存用户使用 swift 选择的图像?

How do i save an image that was selected by a user with swift?

在我制作的应用程序中,用户将 select 一张照片,然后它会出现在图像视图中。我如何将他们 select 编辑的图像保存在 image.xcassets 文件夹中,以便当我重新启动应用程序时,图像仍然存在?

这是我正在使用的代码...

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    @IBAction func Action(sender: AnyObject) {

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {

            let imagePicker:UIImagePickerController = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
            imagePicker.allowsEditing = false

            self.presentViewController(imagePicker, animated: true, completion: nil)




        }

    }






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




           self.dismissViewControllerAnimated(true, completion: nil)

            self.imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage

//how do i save the image in the folder?
        }





    }

你可以这样做

var finalImage : UIImage()

//Your Method
func saveImage () {

       self.imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
       self.finalImage = self.imageView.image as UIImage

       let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
       let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
       if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {

       if paths.count > 0 {
         if let dirPath = paths[0] as? String {
         let readPath = dirPath.stringByAppendingPathComponent("Image.png")
         let image = UIImage(named: readPath)
         let writePath = dirPath.stringByAppendingPathComponent("Image2.png") 
         UIImagePNGRepresentation(self.finalImage).writeToFile(writePath, atomically: true)
    } 
  }
 }


}