如何选择即使退出应用后仍会保留的图像

时间:2018-04-06 08:04:20

标签: swift xcode

我需要帮助请我有一个按钮..而没有图片的UIImage可供用户选择他想要的图片! 一旦用户完全退出应用程序,用户选择的图像保持不变非常重要

我希望用户从图库中选择图片或拍照,然后他选择的图片将显示在个人资料图片上,即使退出应用程序也不会消失..数据将被保存 我可以这样做吗?

    class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet var imageView: UIImageView!
    @IBOutlet var chooseBuuton: UIButton!
    var imagePicker = UIImagePickerController()




    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
        self.dismiss(animated: true, completion: { () -> Void in

        })

        imageView.image = image
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = image
        imageView.contentMode = .scaleAspectFill



        dismiss(animated: true, completion: nil)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

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

1 个答案:

答案 0 :(得分:0)

如果您在应用程序中使用,则需要将图像保存在应用程序存储中,如UserDefaultsDocument directory或数据库中。

最好将图像保存到应用程序文档目录,您可以使用以下方法保存图像:

func saveImage(image : UIImage, imageName: String){
        let documentDirectoryPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        let fileURL = documentDirectoryPath.appendingPathComponent(imageName)
        if let data = UIImageJPEGRepresentation(image, 1.0),
            !FileManager.default.fileExists(atPath: fileURL.path) {
            do {
                try data.write(to: fileURL)
                print("Image saved")
            } catch {
                print("Image save error:", error)
            }
        }
    }

您可以通过以下功能获取图像:

func getImage(imageName: String) -> UIImage?{
        let documentDirectoryPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let fileURL = documentDirectoryPath.appendingPathComponent(imageName)
        let image = UIImage(contentsOfFile: fileURL.path)
        return image
    }

UIImagePickerControllerDelegate委托方法调用函数saveImage中使用image&amp; imageName永久保存文档目录中的图像:

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

    // Do your stuff here, then save image to document directory:
     saveImage(image:image, imageName: "profile.jpg")
    }

要在imageView上显示图像,请从viewDidLoad函数的文档目录中获取图像:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if let image = getImage(imageName: "profile.jpg"){
            imageview.image = image
        }
    }