Reputation: 91
I have a View
with some subviews
on it like Labels
and ImageViews
. The subviews
can have any transformation. I have to convert the View
into PDF using CGContext
. The PDF is created but there is issue in label
positions in final PDF file when I rotate them in View
. I don't know where I am doing wrong. I am using a Category to Draw hierarchy.
Below is my code and Source view and final 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
Upvotes: 0
Views: 77