 
         switch self.contentMode {
         case .ScaleAspectFit, .ScaleAspectFill:
-            let scale = self.contentMode == .ScaleAspectFill ? max(widthRatio, heightRatio) : min(widthRatio, heightRatio)
-            return CGAffineTransformScale(halfTranslate(
-                tx: viewSize.width  - imageWidth  * scale,
-                ty: viewSize.height  - imageHeight  * scale
-                ), scale, scale)
+            let expand = self.contentMode == .ScaleAspectFill
+            let scale = expand ? max(widthRatio, heightRatio) : min(widthRatio, heightRatio)
+            return CGAffineTransformScale(
+                translateWithFactors(
+                    tx: viewSize.width - imageWidth * scale,
+                    ty: viewSize.height - imageHeight * scale,
+                    xFactor: .half,
+                    yFactor: .half
+                ),
+                scale,
+                scale)
         case .ScaleToFill, .Redraw:
             return CGAffineTransformMakeScale(widthRatio, heightRatio)
         case .Center:

    }

    func convertPoint(point: CGPoint, toCoordinateSpace coordinateSpace: UICoordinateSpace) -> CGPoint {
        return viewSpace.convertPoint(CGPointApplyAffineTransform(point, imageToViewTransform), toCoordinateSpace: coordinateSpace)
    }

    func convertRect(rect: CGRect, toCoordinateSpace coordinateSpace: UICoordinateSpace) -> CGRect {
        return viewSpace.convertRect(CGRectApplyAffineTransform(rect, imageToViewTransform), toCoordinateSpace: coordinateSpace)
    }

    func convertPoint(point: CGPoint, fromCoordinateSpace coordinateSpace: UICoordinateSpace) -> CGPoint {
        return CGPointApplyAffineTransform(viewSpace.convertPoint(point, fromCoordinateSpace: coordinateSpace), viewToImageTransform)
    }

    func convertRect(rect: CGRect, fromCoordinateSpace coordinateSpace: UICoordinateSpace) -> CGRect {
        return CGRectApplyAffineTransform(viewSpace.convertRect(rect, fromCoordinateSpace: coordinateSpace), viewToImageTransform)
    }

    // MARK: private

    return CGAffineTransformMakeTranslation(tx * xFactor.rawValue, ty * yFactor.rawValue)
}

class ImageCoordinateSpace : NSObject, UICoordinateSpace {
    var viewSpace : UICoordinateSpace
    var imageSize : CGSize?
