2016-12-25 62 views
1

目標是在pdf頁面中獲取真實幀以識別搜索到的字符串,我使用PDFKitten lib突出顯示搜索到的文本並試圖弄清楚如何獲取突出顯示文本的幀。芯的方法是下一個:仿射變換後獲取真實幀

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 
    CGContextSetFillColorWithColor(ctx, [[UIColor whiteColor] CGColor]); 
    CGContextFillRect(ctx, layer.bounds); 

    // Flip the coordinate system 
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height); 
    CGContextScaleCTM(ctx, 1.0, -1.0); 

    // Transform coordinate system to match PDF 
    NSInteger rotationAngle = CGPDFPageGetRotationAngle(pdfPage); 
    CGAffineTransform transform = CGPDFPageGetDrawingTransform(pdfPage, kCGPDFCropBox, layer.bounds, -rotationAngle, YES); 
    CGContextConcatCTM(ctx, transform); 

    CGContextDrawPDFPage(ctx, pdfPage); 

    if (self.keyword) 
    { 
     CGContextSetFillColorWithColor(ctx, [[UIColor yellowColor] CGColor]); 
     CGContextSetBlendMode(ctx, kCGBlendModeMultiply); 
     for (Selection *s in self.selections) 
     { 
      NSLog(@"layer.bounds = %f, %f, %f, %f", layer.bounds.origin.x, layer.bounds.origin.y, layer.bounds.size.width, layer.bounds.size.height); 
      CGContextSaveGState(ctx); 
      CGContextConcatCTM(ctx, s.transform); 
      NSLog(@"s.frame = %f, %f, %f, %f", s.frame.origin.x, s.frame.origin.y, s.frame.size.width, s.frame.size.height); 
      CGContextFillRect(ctx, s.frame); 
      CGContextRestoreGState(ctx); 
     } 
    } 
} 

層的尺寸是(612.000000,792.000000),但s.frame的大小是(3.110400,1.107000)。我怎樣才能得到真正的框架從rect黃色填充?

+1

frame'的'整體概念是沒有意義的,除非變換是身份。 Docs對此很清楚。 – matt

+0

那麼有沒有辦法做到這一點?對? –

+0

我不知道「那樣做」是什麼意思。我不知道你想做什麼,你甚至沒有解釋過什麼!但是你的_question_是關於'frame'的,而'frame'是一個純粹構造的概念,在變換下是沒有意義的。 – matt

回答

1

正如Matt所說,除非轉換是標識轉換,否則視圖/圖層的框架屬性無效。

如果要使用轉換來轉換某個矩形,那麼CGRect結構就沒有用了,因爲CGRect指定了原點和大小,並且假設矩形的其他3個點從起源。

爲了創建一個轉換後的矩形,需要爲未轉換的框架矩形的左上角,右上角,左下角和右下角點構建4個點,然後將轉換應用於這些點,之前將變換應用於視圖。

請參見功能CGPoint CGPointApplyAffineTransform(CGPoint point, CGAffineTransform t)CGAffineTransform應用於點。

一旦你完成了,你可以使用變換後的點來建立一個包含一個多邊形的bezier路徑,該多邊形是你的變換矩形。 (它可能是也可能不是變形後的矩形,唯一可以肯定的表示它的方式是描述四邊形的4點。)