                 }
                 offset.x = -offset.x
             } else {
+                var centerY = rect.origin.y + rect.size.height / 2.0
+
                 finalSize.width = self.bounds.size.width
                 finalSize.height = size.height / size.width * finalSize.width
-                center.x = finalSize.width / size.width * center.x
-                center.y = finalSize.width / size.width * center.y
-
-                offset.y = center.y - self.bounds.size.height * CGFloat(1-0.618)
+                centerY = finalSize.width / size.width * centerY
+
+                offset.y = centerY - self.bounds.size.height * CGFloat(1-0.618)
                 if offset.y < 0 {
                     offset.y = 0
                 } else if offset.y + self.bounds.size.height > finalSize.height {


        var newImage: UIImage
        if self.debugFaceAware {
            // Draw rectangles around detected faces
            let rawImage = UIImage(cgImage: image.cgImage!)
            UIGraphicsBeginImageContext(size)
            rawImage.draw(at: CGPoint.zero)

            let context = UIGraphicsGetCurrentContext()
            context!.setStrokeColor(UIColor.red.cgColor)
            context!.setLineWidth(3)

            for feature in features[0..<features.count] {
                var faceViewBounds = feature.bounds!
                faceViewBounds.origin.y = size.height - faceViewBounds.origin.y - faceViewBounds.size.height

                context!.addRect(faceViewBounds)
                context!.drawPath(using: .stroke)
            }

            newImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
        } else {
            newImage = image
        }

        rect.size.width = CGFloat(rightBorder) - rect.origin.x
        rect.size.height = CGFloat(bottomBorder) - rect.origin.y

        var offset = CGPoint.zero
        var finalSize = size

        DispatchQueue.main.async {
            if size.width / size.height > self.bounds.size.width / self.bounds.size.height {
                var centerX = rect.origin.x + rect.size.width / 2.0

                finalSize.height = self.bounds.size.height
                finalSize.width = size.width/size.height * finalSize.height
                centerX = finalSize.width / size.width * centerX

                offset.x = centerX - self.bounds.size.width * 0.5
                if offset.x < 0 {
                    offset.x = 0
                } else if offset.x + self.bounds.size.width > finalSize.width {
