I have a set up for a picture selection as follows:
@IBOutlet weak var imageHolder: UIImageView!
@IBAction func addImage(_ sender: UIButton) {
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
pickerController.allowsEditing = true
self.present(pickerController, animated: true, completion: nil)
}
var imageSaved = UIImage()
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismiss(animated: true, completion: nil)
self.imageHolder.image = image
imageSaved = image
}
Selecting the picker brings up all of the pictures on the simulator, then the user can select them. Once it is selected the view returns to the original view and displays the picture in the imageView. It is not saved, though, until a "Save" button is hit in this view. This all works fine. The save button is merely an action that saves the image file to the DB.
I have the viewDidAppear
set up to query the DB and display the image that has been saved as soon as the user moves into that view... this also works well. However, since the viewDidAppear is run multiple times, there becomes an issue when a new image is first selected to replace the old one. The "Save" button deletes the old object and replaces it, so once it has been replaced everything works fine. However, after selecting the image in the picker (and before hitting "Save"... and therefore before that file is in the DB) the view briefly displays the newly selected the image, but the viewDidAppear
runs again and sends it back to the image currently in the DB. Its not technically an error, but it is ugly and I would like it to be fixed but I am not sure how.
This is the viewDidAppear
code also:
override func viewDidAppear(_ animated: Bool) {
let query = PFQuery(className: "ProfilePictures")
query.whereKey("user", equalTo: (PFUser.current()?.objectId!)! as String)
query.findObjectsInBackground { (objects, error) in
if let objectss = objects{
if objectss.count != 0{
for object in objectss{
let imagePulled = object["ProfilePicture"] as! PFFile
imagePulled.getDataInBackground(block: { (data, error) in
if let downloadedImage = UIImage(data: data!){
self.imageHolder.image = downloadedImage
} else{
print(error)
}
})
}
}
}
}
}
Essentially, how do I get the image to only show the newly selected image in that space between selecting it and hitting save?