即使从图库或相机中选择新图像,配置文件图像也不会更新

时间:2018-04-06 06:05:52

标签: ios iphone swift xcode

大家好,感谢所有帮助我的人:)

我想让人们从图库中选择一张图片(如个人资料图片)

并且退出应用后所选图像仍将保留... 当我回到应用程序时,我想看到我选择的图片 我该怎么办呢?

USE-Swift 4

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.

            imageView.backgroundColor = UIColor.lightGray
        }

        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)
        }
    }

2 个答案:

答案 0 :(得分:1)

当您选择任何图像时,可以使用saveImage()方法将图像保存在文档目录中。一种调用getImage()方法,用于在viewDidLoad中获取图像并将其设置在imageView上。

func saveImage(_ image: UIImage) {
    let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Profile.jpg")

    let imageData = UIImageJPEGRepresentation(image, 0.8)
    try? imageData?.write(to: url!)
}

func getImage() -> UIImage? {
    let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Profile.jpg")

    if let data = try? Data(contentsOf: url!) {
        return UIImage(data: data)
    }
    return nil
}

答案 1 :(得分:0)

使用用户默认值并在ViewDidLoad方法中检查用户默认值

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.
                     let possibleOldImagePath = UserDefaults.standard.object(forKey: "path") as! String?
            if let oldImagePath = possibleOldImagePath {
                let oldFullPath = self.documentsPathForFileName(oldImagePath)
                let oldImageData = NSData(contentsOfFile: oldFullPath)
                // here is your saved image:
                imageView = UIImage(data: oldImageData)
            }
                    imageView.backgroundColor = UIColor.lightGray
                }

                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

    // save to userDefaults

            let imageData = UIImageJPEGRepresentation(image, 1)
            let relativePath = "image_\(NSDate.timeIntervalSinceReferenceDate).jpg"
            let path = self.documentsPathForFileName(relativePath)
            imageData.writeToFile(path, atomically: true)
            UserDefaults.standard.set(relativePath, forKey: "path")
            UserDefaults.standard.synchronize()

                    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)
                }
            }

路径方法

 func documentsPathForFileName(name: String) -> String {
            let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);
            let path = paths[0] as String;
            let fullPath = path.stringByAppendingPathComponent(name)

            return fullPath
        }