         print("Scroll view content size while zooming: \(self.scrollView.contentSize)")
     }
     
-//MARK:- Helper Methods
-    func setReferenceFrameForMode(mode: UIViewContentMode) {
+    //MARK:- Helper Methods
+    
+    func setReferenceFrameForMode(mode: UIViewContentMode) -> CGRect{
         
         
         let widthRatio = self.referenceImageView.bounds.size.width / (self.referenceImageView.image != nil ? (self.referenceImageView.image?.size.width)! : self.referenceImageView.bounds.size.width)
         let heightRatio = self.referenceImageView.bounds.size.height / (self.referenceImageView.image != nil ? (self.referenceImageView.image?.size.height)! : self.referenceImageView.bounds.size.height)
+        let referenceFrame : CGRect
         
         switch(mode) {
         case .ScaleAspectFit:

        self.scrollView.scrollEnabled = true
        self.scrollView.showsHorizontalScrollIndicator = false
        self.scrollView.showsVerticalScrollIndicator = false
        
        self.scrollView.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.0)
        self.view.addSubview(self.scrollView)
        
        

            self.imageHeight = scale * (self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0)
            let imageX = ((self.referenceFrame.size.width - self.imageWidth) / 2) + self.referenceFrame.origin.x
            let imageY = ((self.referenceFrame.size.height - self.imageHeight) / 2) + self.referenceFrame.origin.y
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .ScaleAspectFill:
            let scale = max(widthRatio, heightRatio)
            self.imageWidth = scale * (self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0)
            self.imageHeight = scale * (self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0)
            let imageX = ((self.referenceFrame.size.width - self.imageWidth) / 2) + self.referenceFrame.origin.x
            let imageY = ((self.referenceFrame.size.height - self.imageHeight) / 2) + self.referenceFrame.origin.y
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .ScaleToFill:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            referenceFrame = self.referenceImageView.frame
        case .Top:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            let imageX = ((self.referenceFrame.size.width - self.imageWidth) / 2) + self.referenceFrame.origin.x
            let imageY = self.referenceFrame.origin.y
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .Bottom:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            let imageX = ((self.referenceFrame.size.width - self.imageWidth) / 2) + self.referenceFrame.origin.x
            let imageY = (self.referenceFrame.size.height + self.referenceFrame.origin.y) - self.imageHeight
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .Left:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            let imageX = self.referenceFrame.origin.x
            let imageY = ((self.referenceFrame.size.height - self.imageHeight) / 2) + self.referenceFrame.origin.y
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .Right:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            let imageX = (self.referenceFrame.origin.x + self.referenceFrame.size.width) - self.imageWidth
            let imageY = ((self.referenceFrame.size.height - self.imageHeight) / 2) + self.referenceFrame.origin.y
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .Center:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            let imageX = self.referenceImageView.frame.origin.x + ((self.referenceImageView.frame.width - self.imageWidth) / 2)
            let imageY = self.referenceImageView.frame.origin.y + ((self.referenceImageView.frame.height - self.imageHeight) / 2)
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .TopLeft:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            let imageX = self.referenceFrame.origin.x
            let imageY = self.referenceFrame.origin.y
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .TopRight:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            let imageX = self.referenceFrame.origin.x + self.referenceFrame.size.width - self.imageWidth
            let imageY = self.referenceFrame.origin.y
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .BottomLeft:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            let imageX = self.referenceFrame.origin.x
            let imageY = self.referenceFrame.origin.y + self.referenceFrame.size.height - self.imageHeight
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .BottomRight:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            let imageX = self.referenceFrame.origin.x + self.referenceFrame.size.width - self.imageWidth
            let imageY = self.referenceFrame.origin.y + self.referenceFrame.size.height - self.imageHeight
            referenceFrame = CGRectMake(imageX, imageY, self.imageWidth, self.imageHeight)
        case .Redraw:
            self.imageWidth = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.width : 0
            self.imageHeight = self.referenceImageView.image != nil ? self.referenceImageView.image!.size.height : 0
            referenceFrame = self.referenceImageView.frame
        }
        
        return referenceFrame
        
    }
    func centerScrollViewContents() {
