title: Converting Matrix to CGAffineTransform
body: <p>I've developed an app on Android, where you can add images on the screen and pan/rotate/scale them around. Technically, I have a fixed-aspect parent layout, which contains these images (ImageViews). They have set width and height to match_parent. Then, I can save their positions on the server, which I can then load later on same or other device. Basically, I simply save Matrix values. However, translation x/y have absolute values (in pixels), but screen sizes differ, so instead I set them as percentage values (so translation x is divided by parent width and translation y is divided by parent height). When loading images I just multiply translation x/y by parent layout width/height and everything looks the same even on the different device. </p>
  
  <p>Here's how I do it:</p>
  
  <pre><code>float[] values = new float[9];
  parentLayout.matrix.getValues(values);
  
  values[Matrix.MTRANS_X] /= parentLayout.getWidth();
  values[Matrix.MTRANS_Y] /= parentLayout.getHeight();
  </code></pre>
  
  <p>Then in loading function, I simply multiply these values.</p>
  
  <p>Images manipulation is done by using postTranslate/postScale/postRotate with anchor point (for 2-finger gesture it's midpoint between these fingers).</p>
  
  <p>Now, I want to port this app to iOS and use the similar technique to achieve correct images positions on iOS devices as well. CGAffineTransform seems to be very similar to Matrix class. The order of fields is different, but they seem to work the same way. </p>
  
  <p>Let's say this array is the sample values array from Android app:</p>
  
  <pre><code>let a: [CGFloat] = [0.35355338, -0.35355338, 0.44107446, 0.35355338, 0.35355338, 0.058058262]
  </code></pre>
  
  <p>I don't save last 3 fields, because they're always 0, 0 and 1 and on iOS they can't be even set (documentation also states it's [0, 0, 1]). So this is how the "conversion" looks like:</p>
  
  <pre><code>let tx = a[2] * width //multiply percentage translation x with parent width
  let ty = a[5] * height //multiply percentage translation y with parent height
  let transform = CGAffineTransform(a: a[0], b: a[3], c: a[1], d: a[4], tx: tx, ty: ty)
  </code></pre>
  
  <p>However, this only work for very simple cases. For other ones image is usually misplaced or even completely messed up.</p>
  
  <p>I've noticed that, by default, on Android I have anchor point in [0, 0], but on iOS it's image's center. </p>
  
  <p>For example:</p>
  
  <pre><code>float midX = parentLayout.getWidth() / 2.0f;
  float midY = parentLayout.getHeight() / 2.0f;
  
  parentLayout.matrix.postScale(0.5f, 0.5f, midX, midY);
  </code></pre>
  
  <p>Gives the following matrix:</p>
  
  <pre><code>0.5, 0.0, 360.0, 
  0.0, 0.5, 240.0, 
  0.0, 0.0, 1.0
  </code></pre>
  
  <p>[360.0, 240.0] is simply a vector from the left-top corner. </p>
  
  <p>However, on iOS I don't have to provide mid point, because it's already transformed around center:</p>
  
  <pre><code>let transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
  </code></pre>
  
  <p>and it gives me:</p>
  
  <pre><code>CGAffineTransform(a: 0.5, b: 0.0, c: 0.0, d: 0.5, tx: 0.0, ty: 0.0)
  </code></pre>
  
  <p>I've tried setting a different anchor point on iOS:</p>
  
  <pre><code>parentView.layer.anchorPoint = CGPoint(x: 0, y: 0)
  </code></pre>
  
  <p>However, I cannot get the right results. I've tried translation by -width * 0.5, -height * 0.5, scale and translate back, but I don't get the same result as on Android.</p>
  
  <p>In short: how can I modify Matrix from Android to CGAffineTransform from iOS to achieve the same look?</p>
  
  <p>I'm also attaching as-simple-as-possible demo projects. The objective is to copy output array from Android into "a" array in iOS project and modify CGAffineTransform calculation (and/or parentView setup) in way that image looks the same on both platforms no matter what's the log output from Android.</p>
  
  <p>Android:
  <a href="https://github.com/piotrros/MatrixAndroid" rel="nofollow noreferrer">https://github.com/piotrros/MatrixAndroid</a></p>
  
  <p>iOS:
  <a href="https://github.com/piotrros/MatrixIOS" rel="nofollow noreferrer">https://github.com/piotrros/MatrixIOS</a></p>
  
  <p><strong>edit:</strong></p>
  
  <p>@Sulthan solution is working great! Althought, I've come up with a subquestion of reversing the process, which I also need. Using on his answer, I've tried to incorporate into my app:</p>
  
  <pre><code>let savedTransform = CGAffineTransform.identity
          .concatenating(CGAffineTransform(translationX: -width / 2, y: -height / 2))
          .concatenating(
              self.transform
          )
          .concatenating(CGAffineTransform(translationX: width / 2, y: height / 2))
  
  let tx = savedTransform.tx
  let ty = savedTransform.ty
  
  let t = CGAffineTransform(a: savedTransform.a, b: savedTransform.b, c: savedTransform.c, d: savedTransform.d, tx: tx / cWidth, ty: ty / cHeight)
  
  placement.transform = [Float(t.a), Float(t.c), Float(t.tx), Float(t.b), Float(t.d), Float(t.ty), 0, 0, 1]
  </code></pre>
  
  <p>placement is just a class to hold "Android" version of matrix. cWidth / cHeight is width/height of parent. I divide tx/ty by them to get percentage width/height, just like I do on Android. </p>
  
  <p>However, it doesn't work. tx/ty are wrong.</p>
  
  <p>For input: </p>
  
  <blockquote>
    <p>[0.2743883, -0.16761175, 0.43753636, 0.16761175, 0.2743883,
    0.40431038, 0.0, 0.0, 1.0]</p>
  </blockquote>
  
  <p>it gives back:</p>
  
  <blockquote>
    <p>[0.2743883, -0.16761175, 0.18834576, 0.16761175, 0.2743883, 0.2182884,
    0.0, 0.0, 1.0]</p>
  </blockquote>
  
  <p>so everything else other then tx/ty is fine. Where's the error?</p>