0

我有View一些subviewsLabelsImageViewssubviews可以有任何变换。我必须使用. View_ PDF已创建,但当我将它们旋转. 我不知道我在哪里做错了。我正在使用类别来绘制层次结构。CGContextlabelView

下面是我的代码和源视图以及最终的PDF原始视图 已创建 PDF 文件

UIGraphicsBeginPDFContextToFile(myPathDocs, CGRectMake(0, 0, 612, 792), nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),bgColor.CGColor);
CGContextFillRect(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, 612, 792));
    [_pdfView drawHierarchy];
UIGraphicsEndPDFContext();


// Category
#import "UIView+HierarchicalDrawing.h"


@implementation UIView (HierarchicalDrawing)

- (void)drawHierarchy {

    [self layoutSubviews];

    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSaveGState(c);

    CGContextTranslateCTM(c, self.frame.origin.x, self.frame.origin.y);

    if (self.backgroundColor != nil) {
        CGContextSetFillColorWithColor(c, [self.backgroundColor CGColor]);
        CGContextFillRect(c, self.bounds);
    }
    if ([self isKindOfClass:[UIImageView class]]) {
        if(self.layer.shadowColor) {
            CGContextSetShadowWithColor(c, self.layer.shadowOffset, 0.0, self.layer.shadowColor);
        }
    }

    if ([self isKindOfClass:[UILabel class]]) {

        CGPoint centrePoint = self.center;
        CGFloat angle = atan2f(self.transform.b, self.transform.a);

        CGContextRotateCTM(c, angle); 

    }

    [self drawRect:self.bounds];

    if(self.clipsToBounds) CGContextClipToRect(c, self.bounds);

    for(UIView *v in self.subviews) {
        if(v.hidden) continue;
        [v drawHierarchy];
    }
    //    NSLog(@"counter is %d",counter);
    CGContextRestoreGState(c);
}
@end
4

0 回答 0