Updating PhotoFilterViewController to save photos

The code that enables PhotoFilterViewController to save photos is similar to the code you implemented in ReviewFormViewController for saving reviews. You will now update PhotoFilterViewController to save photos when the Save button is tapped. Do the following steps:

  1. Click PhotoFilterViewController.swift in the Project navigator, and add the following method inside the private extension:
func checkSavedPhoto() {
if let img = self.imgExample.image {
var item = RestaurantPhotoItem()
item.photo = generate(image: img, ratio: CGFloat(102))
item.date = NSDate() as Date
item.restaurantID = selectedRestaurantID
let manager = CoreDataManager()
manager.addPhoto(item)

dismiss(animated: true, completion: nil)
}
}

checkSavedPhoto() first checks to see if the image property of imgExample is set. If it is, a RestaurantPhotoItem instance with the data from that image will be saved to the persistent store.

  1. You need to trigger this method when the Save button is tapped. Add the following method inside the private extension:
@IBAction func onSaveTapped(_ sender: AnyObject) {
DispatchQueue.main.async {
self.checkSavedPhoto()
}
}

Note the use of DispatchQueue.main.async here. What this does is perform the checkSavedPhoto() operation in the background so that the user interface does not become unresponsive.

  1. You need to assign this action to the Save button. Open PhotoFilter.storyboard, and click the Photo Filter View Controller icon in the Photo Filter View Controller Scene. Open the Connections inspector. Drag from the onSaveTapped action to the Save button, as follows:

Before you can save, you need to pass the restaurant identifier to PhotoFilterViewController. Do the following steps:

  1. Click RestaurantDetail.storyboard in the Project navigator and select the segue used to go to the Photo Filter screen, as follows:

  1. In the Attributes inspector, set Identifier under Storyboard Segue to showPhotoFilter, as follows (hit Return when done):


  1. Click RestaurantDetailViewController.swift in the Project navigator.
  1. Update the prepare(for:sender:) method, as follows:
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if let identifier = segue.identifier {
switch identifier {
case Segue.showReview.rawValue:
showReview(segue: segue)
case Segue.showPhotoFilter.rawValue:
showPhotoFilter(segue: segue)
default:
print("Segue not added")
}
}
}

This makes the prepare(for:sender:) method check to see if the segue has the showPhotoFilter segue identifier. If it does, the showPhotoFilter(segue:) method is executed when transitioning from the Restaurant Detail screen to the Photo Filter screen. There will be an error because showPhotoFilter(segue:) has not been implemented yet.

  1. Add the showPhotoFilter(segue:) method after the showReview() method inside your private extension:
func showPhotoFilter(segue:UIStoryboardSegue){
guard let navController = segue.destination as?
UINavigationController,
let viewController = navController.topViewController as?
PhotoFilterViewController else {
return
}
viewController.selectedRestaurantID = selectedRestaurant?.restaurantID
}

This sets the PhotoFilterViewController's restaurantID property to the identifier of the selected restaurant.

  1. Build and run your project, set a location, and tap All. Tap a restaurant, and tap the Add Photo button. Select a photo, apply a filter and tap Save:

The photo will be saved to the persistent storage, and you will be brought back to the Restaurant Detail screen.

At this point, you can save reviews and photos. Fantastic!

Next, you will add code to load the reviews and photos from the persistent store to be displayed on the Restaurant Detail screen. You will do this in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.199.138