Wrapping Up ImageStore

Now that the ImageStore can store images and instances of Item have a key to get an image (Figure 15.14), you need to teach DetailViewController how to grab the image for the selected Item and place it in its imageView.

The DetailViewController’s view will appear when the user taps a row in ItemsViewController and when the UIImagePickerController is dismissed. In both of these situations, the imageView should be populated with the image of the Item being displayed. Currently, it is only happening when the UIImagePickerController is dismissed.

In DetailViewController.swift, make this happen in viewWillAppear(_:).

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    nameField.text = item.name
    serialNumberField.text = item.serialNumber
    valueField.text =
        numberFormatter.string(from: NSNumber(value: item.valueInDollars))
    dateLabel.text = dateFormatter.string(from: item.dateCreated)

    // Get the item key
    let key = item.itemKey

    // If there is an associated image with the item
    // display it on the image view
    let imageToDisplay = imageStore.image(forKey: key)
    imageView.image = imageToDisplay
}

Build and run the application. Create an item and select it from the table view. Then, tap the camera button and take a picture. The image will appear as it should. Pop out from the item’s details to the list of items. Unlike before, if you tap and drill down to see the details of the item you added a picture to, you will see the image.

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

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