Hi! At main.storyboard I have UIImageView and button. I want to save my image witch I took via camera to file system. But when I loading my app again, this image disappeared. Tell me please what I done wrong? This is my code:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBAction func useCamera(_ sender: UIButton) {
let picker = UIImagePickerController() picker.sourceType = .camera
self.present(picker, animated: true, completion: nil) picker.delegate = self }
@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. }
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage picker.dismiss(animated: true, completion: nil)
if imageView.image == UIImage(named: "example.jpg") { if let data = UIImageJPEGRepresentation(imageView.image!, 0.8) { let filename = getDocumentsDirectory().appendingPathComponent("copy.png") try? data.write(to: filename) } } }
func getDocumentsDirectory() -> URL { let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) let documentsDirectory = paths[0] return documentsDirectory }
}