Golden Challenged solved with UIImageView and Silver with UIAlertConroller

It was a bit puzzle for me to solve the Golden Challenge in part how to draw crosshairs. To be 100% native solution you need to create a custom UIView where to use UIBezierPath to draw the crosshairs, the way we didn’t learn and which is a magnitude of effort. So I decided to download an image of crosshairs put in the Assets.xcassets and add it to the cameraOverlayView property as a UIImageView in the middle of the image capture area.

The sizes are hardcoded based on the image size.

@IBAction func takePicture(_ sender: UIBarButtonItem) {
    let imagePickerController = UIImagePickerController()
    if UIImagePickerController.isSourceTypeAvailable(.camera) {
        imagePickerController.sourceType = .camera
    } else {
        imagePickerController.sourceType = .photoLibrary
    }
    imagePickerController.delegate = self
    imagePickerController.allowsEditing = true
    
    //   ***** \/ The Golden Challenge Solution \/ ****

    let image = UIImage(named: "crosshair") //Downloaded from web to Assets.xcassets
    let iView = UIImageView(frame: CGRect(x: imagePickerController.cameraOverlayView!.bounds.midX - 40, y: imagePickerController.cameraOverlayView!.bounds.midY - 70, width: 80, height: 80))
    iView.image = image
    imagePickerController.cameraOverlayView = iView
    
    //  ***** /\ The Golden Challenge Solution /\ ****

    present(imagePickerController, animated: true, completion: nil)
}

Enabling a button to delete the image just by a click is abrupt and not user friendly. Following the Apple’s HIG you need to ask for user confirmation of destructive action. Perfect solution is to embed such actions in UIAlertController.

@IBAction func deletePicture(_ sender: UIBarButtonItem) {
    if imageView.image != nil {
        let controller = UIAlertController(title: "Delete Photo", message: "Are you sure to delete permanently the item's photo?", preferredStyle: .actionSheet)
        let oKAction = UIAlertAction(title: "OK", style: .destructive) { (action) in
            self.imageView.image = nil
            self.imageStore.deleteImage(forKey: self.item.itemKey)
            self.trashButton.isEnabled = false
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        controller.addAction(oKAction)
        controller.addAction(cancelAction)
        present(controller, animated: true, completion: nil)
    }
}