EarlGrey
EarlGrey

Reputation: 2543

getting pdf data out of pdf context

I'm very new to the world of contexts.

  1. I create a pdfcontext using:

    NSMutableData* pdfData = [ [[NSMutableData alloc] initWithLength:1000] retain];
    CGRect bounds = (CGRectMake(100, 100, 100, 100));
    NSDictionary* documentInfo = nil;
    UIGraphicsBeginPDFContextToData (pdfData,
                                 bounds,
                                 documentInfo);
    UIGraphicsBeginPDFPage();
    
  2. Then I draw to it with different classes

  3. Then I issue

    UIGraphicsEndPDFContext();
    

when I'm done drawing to it.

What I'm not clear on is how do I get the pdfData out of the context so I can send it back to my caller as NSData. Any help appreciated. I'm assuming in my approach that the pdfData gets retained within the context. Thank you!

Upvotes: 0

Views: 337

Answers (1)

Kurt Revis
Kurt Revis

Reputation: 27994

When you call UIGraphicsEndPDFContext, it puts the PDF data into the pdfData object that you provided. Since an NSMutableData is a subclass of NSData, you can just return it to your caller. There is no need to do anything else.

Also, don't do this:

NSMutableData* pdfData = [[[NSMutableData alloc] initWithLength:1000] retain];

alloc/init returns a retained object. There is no need to retain it again. Also, there's no need to specify a size for the data -- it will get expanded automatically. So just do this:

NSMutableData* pdfData = [[NSMutableData alloc] init];

Upvotes: 1

Related Questions