从已转换的CGContext中检索CGRect并应用于UIView

时间:2012-03-15 18:38:59

标签: cgcontext cgaffinetransform cgrect

我正在将PDF页面绘制成CGContext。

为了正确绘制它,我将一些转换应用于上下文。

pdf页面呈现的rect小于视图的rect。

我想创建第三个视图,该视图与视图中呈现pdf的部分完全相同。

我的解决方案有效,但并非完全如此。有时(很多时候)矩形是错误的。

这就是我在做的事情:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
      CGContextSaveGState(context);
      // apply transforms to context
      // draw pdf page

      CGRect calculatedFromRect = CGRectApplyAffineTransform(pageRect, CGContextGetCTM(context));
      CGContextRestoreGState(context);

      // now draw a green rect to test the frame on a not transformed context
      GContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
      CGContextFillRect(context, calculatedFromRect);

      self.thirdView.frame = calculatedFromRect;
 }

第三个视图是红色的。当两个rects(视图和绘图)相等时,我在屏幕上看到一个棕色的矩形(红色,绿色矩形顶部有alpha)。但有时我可以看到它们彼此分开(偏移和大小差异......当发生这种情况时,第三个View.frame比calcularedRect大)。

由于所有涉及的视图具有相同的大小和坐标,因此不使用convertRect:fromView:转换坐标应该不是问题。但我尝试了这个,结果是一样的。

0 个答案:

没有答案