Bronze Challenge: Editing an Image
In DetailViewController.swift, in function takePicture(_ add the code below before presenting the image picker.
imagePicker.allowsEditing = true
After taking a photo, you can now zoom and crop the photo. To use the edited image, instead of the OriginalImage, update imagePickerController(_:didFinishPickingMediaWithInfo:)
// let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let image = info[UIImagePickerControllerEditedImage] as! UIImage
Silver Challenge: Removing an Image
In DetailViewController.swift, add the code below to class DetailViewController
var flexibleSpaceBarButton: UIBarButtonItem!
var removePictureBarButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
flexibleSpaceBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
removePictureBarButton = UIBarButtonItem(
barButtonSystemItem: .trash,
target: self, action: #selector(removePicture(_:)))
// Locate the toolbar at bottom and add to it the barButtons
for subView in self.view.subviews {
if let toolbar = subView as? UIToolbar {
var toolbarItems = toolbar.items
toolbarItems?.append(flexibleSpaceBarButton)
toolbarItems?.append(removePictureBarButton)
toolbar.setItems(toolbarItems, animated: true)
break
}
}
}
@IBAction func removePicture(_ sender: UIBarButtonItem) {
imageStore.deleteImage(forKey: item.itemKey)
imageView.image = nil
removePictureBarButton.isEnabled = false
}
Add the code below to the end of viewWillAppear(_
removePictureBarButton.isEnabled = imageView.image != nil
Add the code below to the end of imagePickerController(_:didFinishPickingMediaWithInfo:)
removePictureBarButton.isEnabled = true
Gold Challenge: Camera Overlay
Note that the overlayView property can be accessed only when the source type of the image picker is set to camera. In DetailViewController.swift, update takePicture(_ as below
@IBAction func takePicture(_ sender: UIBarButtonItem) {
let imagePicker = UIImagePickerController()
// If the device has a camera, take a picture; otherwise,
// just pick from photo library
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
let overlayView = UIView(frame: imagePicker.cameraOverlayView!.frame)
let crosshairLabel = UILabel()
crosshairLabel.text = "+"
crosshairLabel.font = UIFont.systemFont(ofSize: 50)
crosshairLabel.translatesAutoresizingMaskIntoConstraints = false
overlayView.addSubview(crosshairLabel)
crosshairLabel.centerXAnchor.constraint(equalTo: overlayView.centerXAnchor).isActive = true
crosshairLabel.centerYAnchor.constraint(equalTo: overlayView.centerYAnchor).isActive = true
// To avoid blocking the underneath default camera controls
overlayView.isUserInteractionEnabled = false
imagePicker.cameraOverlayView = overlayView
} else {
imagePicker.sourceType = .photoLibrary
}
imagePicker.delegate = self
imagePicker.allowsEditing = true
// Place image picker on the screen
present(imagePicker, animated: true, completion: nil)
}