Gold Challenge Solution (Using UIView)


#1

DetailViewController.swift

@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
        // Gold Challenge
        let crosshairView = CrosshairView(frame: self.view.frame)
        crosshairView.backgroundColor = UIColor.clear
        imagePicker.cameraOverlayView = crosshairView
        //
    } else {
        imagePicker.sourceType = .photoLibrary
    }
    
    imagePicker.delegate = self
    imagePicker.allowsEditing = true
    
    // Place image picker on the screen
    present(imagePicker, animated: true, completion: nil)
}

CrosshairView.swift

import UIKit

class CrosshairView: UIView {

var crosshairColor : UIColor = UIColor.white.withAlphaComponent(0.8)
var crosshairWidth : CGFloat = 3.0

// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
    // Drawing code
    Swift.print("Crosshair View Working!")
    
    let centerX : CGFloat = (rect.width / 2.0)
    let centerY : CGFloat = (rect.height / 2.0) - (0.055 * rect.height)
    
    crosshairColor.set()
    
    let horizontalLine = UIBezierPath()
    horizontalLine.lineWidth = self.crosshairWidth
    horizontalLine.move(to: CGPoint(x: centerX - 7.0, y: centerY))
    horizontalLine.addLine(to: CGPoint(x: centerX - 2.0, y: centerY))
    horizontalLine.move(to: CGPoint(x: centerX + 2.0, y: centerY))
    horizontalLine.addLine(to: CGPoint(x: centerX + 7.0, y: centerY))
    horizontalLine.stroke()
    
    let verticalLine = UIBezierPath()
    verticalLine.lineWidth = self.crosshairWidth
    verticalLine.move(to: CGPoint(x: centerX, y: centerY - 7.0))
    verticalLine.addLine(to: CGPoint(x: centerX, y: centerY - 2.0))
    verticalLine.move(to: CGPoint(x: centerX, y: centerY + 2.0))
    verticalLine.addLine(to: CGPoint(x: centerX, y: centerY + 7.0))
    verticalLine.stroke()
}

}


#2

Wow google classroom this is great man. Thanks for sharing this. Keep up gimp the good work.

Regards,
Brian.