0

I'm using below code to create PDF from a UIView which already has subviews. I'm getting all subviews and redrawing in pdf context. But I'm not able to transform UIImageView. Below is my code

NSMutableData *pdfData = [NSMutableData data];
CGRect pageFrame = CGRectMake(0, 0, aView.frame.size.width, aView.frame.size.height);
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
if([view isKindOfClass:[UIImageView class]]) {
    UIImageView *imgv = (UIImageView *)view;
    [imgv.image  drawInRect:CGRectMake(view.frame.origin.x + imgv.frame.origin.x, view.frame.origin.y + imgv.frame.origin.y, imgv.frame.size.width, imgv.frame.size.height)];
}
UIGraphicsEndPDFContext();

Even though image is rotated in my UIView but pdf showing no rotation. How do I transform it to 90 degree in pdf context?

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Hiren Prajapati
  • 717
  • 3
  • 10
  • 25
  • hey you have a uiview which contain a image and data or only image? –  Apr 24 '18 at 11:33
  • @RB1509 UIView have other UIViews as subview and also has UIImageView. First I display view on screen and getting all those views to draw in graphics context for pdf. – Hiren Prajapati Apr 24 '18 at 11:55
  • can you try sudhir's answer?yes or no. –  Apr 24 '18 at 11:57
  • @RB1509 It copies the whole UIView but lower's the resolution. So I guess I need to create all objects individually. – Hiren Prajapati Apr 24 '18 at 12:18
  • if you have a main view and your subview inside of your main view it will make pdf of only your main view. –  Apr 24 '18 at 12:20
  • I'm using UIGraphicsBeginPDFContextToData and getting all UIViews from main view and use drawInRect() – Hiren Prajapati Apr 24 '18 at 13:05

2 Answers2

0

Note that the following method creates just a bitmap of the view; it does not create actual typography.

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

Also make sure you import: QuartzCore/QuartzCore.

0

If you just need to draw some element with rotation, try to use matrix transformation functions for current context (it will be PDF context).

CGContextRotateCTM(currentContext, M_PI / 2);
  • if I use UIGraphicsGetCurrentContext(), this image does not even display. I have not used context variable when I start drawing pdf. I think UIGraphicsBeginPDFContextToData is affecting. What you say? – Hiren Prajapati Apr 24 '18 at 11:11
  • It rotates around (0;0) origin. Probably you forgot to translate (or scale) CTM after rotation? You can find good samples on how rotation works here: https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/HTML-canvas-guide/Translation,Rotation,andScaling/Translation,Rotation,andScaling.html – ashraine Apr 27 '18 at 14:50
  • And one more great guide about transformations: https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html – ashraine Apr 27 '18 at 14:55