3

Is there a way of converting UIKit coordinates (0,0 top left) to Quartz/CoreImage (0,0 bottom left)? Can't find anything swift related like this on here.

Adam Allard
  • 403
  • 1
  • 5
  • 11

2 Answers2

5

You can use affine transformation matrix, this snipped is taken from a code of mine to convert from Core Image / Core graphics to UIKit:

CGAffineTransform t = CGAffineTransformMakeScale(1, -1);
t = CGAffineTransformTranslate(t,0, -imageView.bounds.size.height);

Basically you need to:

  • Negate the y axis
  • translate origin by the view height

After the you can use those geometric functions to calculate your rect or points

CGPoint pointUIKit = CGPointApplyAffineTransform(pointCI, t);
CGRect rectUIKit = CGRectApplyAffineTransform(rectCI, t);

In Swift 3.x:

var t = CGAffineTransform(scaleX: 1, y: -1)
t = t.translatedBy(x: 0, y: -imageView.bounds.size.height)
let pointUIKit = pointCI.applying(t)
let rectUIKIT = rectCI.applying(t)
Goldyy
  • 5
  • 4
Andrea
  • 26,120
  • 10
  • 85
  • 131
2

With the origin in the bottom left versus the top left, you need to do nothing with the X axis, but you need to flip the right axis. UIKit (or in this case, Core Graphics) uses CGPoints. Core Image typically uses a CIVectors, which can have 2, 3, or 4 axis angles.

Here's a simple function that will turn a CGPoint(X,Y) into a CIVector(X,Y):

func createVector(_ point:CGPoint, image:CIImage) -> CIVector {
    return CIVector(x: point.x, y: image.extent.height - point.y)
}
  • Yes, as dfd says. The only difference between the coordinate values is the view's height in the y axis. – Magnas Mar 02 '17 at 14:11