How can I use a picture from the Photo Library in my app? I have done the tutorials about credating a photo app. That all works fine. I have a UIPickerController
, can take a picture with the camera, save it to the library, or select an image from the library, which is then put onto the screen. But...
What I want is that the user selects a picture, remember the name of the picture or the number or something, and then on another page use this picture from the library.
Like selecting an avatar. Saving the picture somewhere and whenever the user enters the profile page, open up this avatar picture previously selected. So what I need is the "name"(represenation) of the picture, but I can't find that. How do I do that?
(the code below is just the working cam/lib app part, but it saves the picture without name, that's the problem: How do I find out what picture was saved earlier?) P.S. Sorry, variable names are in Dutch.
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var plaatje: UIImageView!
@IBAction func saveknop(_ sender: UIButton) {
let imageData = UIImageJPEGRepresentation(plaatje.image!, 0.6)
let compressedfoto = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedfoto!, nil, nil, nil)
saveNotice()
}
@IBAction func cameraknop(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func biepknop(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
plaatje.image = image
self.dismiss(animated: true, completion: nil)
}
func saveNotice() {
let alert = UIAlertController(title: "foto genomen", message: "je foto is bewaard in de biep", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(defaultAction)
present(alert, animated: true, completion: nil)
}
}
Struggeling with the suggestions, I found another solution, that is strangely simple. But it works! Weird. It is not the best one, but for now I'll use this, untill I understand everyuthiong about the directorys and filesystem on the iphone.
P.S. credit goes to: http://theswiftguy.com/index.php/2016/11/10/how-to-save-an-image-locally-in-xcode-8-swift-3-0/
//Encoding
let image = UIImage(named: "dog.png")
let imageData:NSData = UIImagePNGRepresentation(image!)! as NSData
//Saved image
UserDefaults.standard.set(imageData, forKey: "savedImage")
//Decode
let data = UserDefaults.standard.object(forKey: "savedImage") as! NSData
myImageView.image = UIImage(data: data as Data)