I created a custom class UIView so that I can a portion of an image from my viewController class. I created the UIView that I would like to to magnify in the viewController class and named its @IBOutlet property xRaySuperView: UIView! I embeded the Magnifier: UIView custom class view in this view using the storyboard.
I then instantiated a viewController object in the custom Magnifier class. However, when I try to transform the layer of the xRaySuperView and render it in context, the build fails and states 'unexpected nil while unwrapping optional value'.
It appears that my xRaySuperView is not passing it layer value to the Magnifier class.? (IT is th
It is the last line creating the error during build.
import UIKit
import QuartzCore
class MagnifierView: UIView {
var viewController: ViewController!
// First Step: Initialize the UIView
required init?(coder aDecoder: NSCoder) {
// Create instance of ViewController Class
let viewController = ViewController()
super.init(coder: aDecoder)
// Makes the circle-shape outline.
self.layer.borderColor = UIColor.blue.cgColor
self.layer.borderWidth = 4
self.layer.cornerRadius = 65 //1/2 the width of the frame
self.layer.masksToBounds = true
}
// Perform transforms on the xray view
override func draw(_ rect: CGRect) {
let xrayContext = UIGraphicsGetCurrentContext()
xrayContext?.translateBy(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
xrayContext?.scaleBy(x: 1.5, y: 1.5)
self.viewController.xRaySuperView.layer.render(in: xrayContext!)
}
}