title: How to find UIView's frame after transform rotating
body: <p>I have a very simple <code>UIImageView</code> which is contained in a container view. They are the same size, no auto layout used. When user presses a button, <code>UIImageView</code> should be rotated by 90 degrees. When <code>UIImageView</code> (not <code>UIImageView.image</code>) is rotated it should fit its superview (<code>.ScaleAspectFit</code>).
  So far I've figured out the easiest part: transform rotation.</p>
  
  <pre><code>    self.currentRotation += 90
      if self.currentRotation &gt;= 360 {
          self.currentRotation = 0
      }
  
      let angleInRadians: Double
      switch self.currentRotation {
      case 0:
          angleInRadians = 0
      case 90:
          angleInRadians = M_PI_2
      case 180:
          angleInRadians = M_PI
      case 270:
          angleInRadians = M_PI + M_PI_2
      default:
          angleInRadians = 0
          break;
      }
      let newTransform = CGAffineTransformRotate(CGAffineTransformIdentity, CGFloat(angleInRadians))
  </code></pre>
  
  <p>By getting some information from <a href="https://stackoverflow.com/questions/19523487/find-frame-coordinates-after-uiview-transform-is-applied-cgaffinetransform">this question</a>, I was able to write Swift versions of helper extensions, but I still can't figure out how to find UIImageView's frame.</p>
  
  <pre><code>extension UIView {
      var originalFrame: CGRect {
          let currentTransform = self.transform
          self.transform = CGAffineTransformIdentity
          let originalFrame = self.frame
          self.transform = currentTransform
          return originalFrame
      }
  
      var transformedTopLeftPoint: CGPoint {
          return self.transformedPointForOriginalPoint(originalFrame.topLeftPoint)
      }
  
      var transformedTopRightPoint: CGPoint {
          return self.transformedPointForOriginalPoint(originalFrame.topRightPoint)
      }
  
      var transformedBottomLeftPoint: CGPoint {
          return self.transformedPointForOriginalPoint(originalFrame.bottomLeftPoint)
      }
  
      var transformedBottomRightPoint: CGPoint {
          return self.transformedPointForOriginalPoint(originalFrame.bottomRightPoint)
      }
  
      // helper to get point offset from center
      func centerOffset(point: CGPoint) -&gt; CGPoint {
          return CGPoint(x: point.x - self.center.x, y: point.y - self.center.y)
      }
  
      // helper to get point back relative to center
      func pointRelativeToCenter(point: CGPoint) -&gt; CGPoint {
          return CGPoint(x: point.x + self.center.x, y: point.y + self.center.y)
      }
  
      // helper to get point relative to transformed coords
      func transformedPointForOriginalPoint(point: CGPoint) -&gt; CGPoint {
          // get offset from center
          let offset = self.centerOffset(point)
          // get transformed point
          let transformedPoint = CGPointApplyAffineTransform(offset, self.transform)
          // make relative to center
          return self.pointRelativeToCenter(transformedPoint)
      }
  }
  
  extension CGRect {
      var topLeftPoint: CGPoint {
          return self.origin
      }
  
      var topRightPoint: CGPoint {
          return CGPoint(x: self.origin.x + self.size.width, y: self.origin.y)
      }
  
      var bottomLeftPoint: CGPoint {
          return CGPoint(x: self.origin.x, y: self.origin.y + self.size.height)
      }
  
      var bottomRightPoint: CGPoint {
          return CGPoint(x: self.origin.x + self.size.width, y: self.origin.y + self.size.height)
      }
  }
  </code></pre>
  
  <p>For better clarification, I'm attaching an image. </p>
  
  <p><a href="https://i.stack.imgur.com/yFuSF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yFuSF.png" alt="image for better understanding"></a></p>
  
  <blockquote>
    <p>It shows a rotated view on top of its original frame (the smallest of
    the outlines) and the updated frame (the largest gray outline). The
    circles indicate the view's top-right corner before and after
    rotation. After the transform is applied, the frame updates to the
    minimum bounding box that encloses the view. Its new origin (the
    top-left corner of the gray outside box) has essentially nothing to do
    with the original view origin (the top-left corner of the black
    unrotated inner box). iOS does not provide a way to retrieve that
    adjusted point. Here are a series of view methods that perform that
    math for you. (UIView extension above)</p>
  </blockquote>
  
  <p>I need to find the gray box's frame. But even with those methods I still can't figure out how to do that. What's the formula/algorithm to achieve what I want?</p>