Ios UIImagePickerController在Swift中的图像

Ios UIImagePickerController在Swift中的图像,ios,swift,uiimage,uiimagepickercontroller,Ios,Swift,Uiimage,Uiimagepickercontroller,我在程序中使用UIImagePickerController,它有效地更改了我添加的imageview的图像。但是,每当我重新启动此应用程序并返回主屏幕时,它都会自动重置为我以前使用过的默认图像,而不是用户选择的图像。如何使它记录上次使用的图像,并在每次程序启动时重新加载 var imagePicker = UIImagePickerController() func chooseImage(_ sender: Any) { //function called with button pre

我在程序中使用UIImagePickerController,它有效地更改了我添加的imageview的图像。但是,每当我重新启动此应用程序并返回主屏幕时,它都会自动重置为我以前使用过的默认图像,而不是用户选择的图像。如何使它记录上次使用的图像,并在每次程序启动时重新加载

 var imagePicker = UIImagePickerController()
 func chooseImage(_ sender: Any) { //function called with button press

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.allowsEditing = true

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in

        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)
        }else{
            print("Camera not available")
        }


    }))

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
        imagePickerController.sourceType = .photoLibrary
        self.present(imagePickerController, animated: true, completion: nil)
    }))

    actionSheet.addAction(UIAlertAction(title: "Default", style: .default, handler: { (action:UIAlertAction) in
        self.avatarImageView.image = UIImage(named: "Avatar.png")


    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(actionSheet, animated: true, completion: nil)


}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let image = info[UIImagePickerControllerEditedImage] as! UIImage

    avatarImageView.image = image

    picker.dismiss(animated: true, completion: nil)



}

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

由于应用程序内存不足,您需要某种持久性机制来保存图像。最简单的方法是将图像存储在
UserDefaults
中。这可以通过以下方式实现:

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

    let image = info[UIImagePickerControllerEditedImage] as! UIImage
    avatarImageView.image = image
    UserDefaults.standard.set(UIImagePNGRepresentation(image), forKey: "avatarImage")    

    picker.dismiss(animated: true, completion: nil)
}
然后,当您重新打开应用程序时,您需要检查您之前是否在
UserDefaults
中保存了avatarImage,并从中加载:

// Could be in viewDidLoad or wherever else you load your image
override func viewDidLoad() {

    if let imageData = UserDefaults.standard.object(forKey: "avatarImage") as? Data {
        avatarImageView.image = UIImage(data: imageData)
    }
}

我想你指的是实际的应用程序重启(即内存耗尽的地方),而不是点击主屏幕,然后将应用程序重新启动?你需要将图像保存在某个地方,并在下次应用程序加载时加载它starts@LeoDabus我该怎么做?请引导我。将其另存为jpeg。您需要有某种持久层。您可以使用数据库、核心数据甚至NSUserDefault。稍后我将尝试整理一个示例。