2010-04-06 10 views
2

J'essaie de prendre une image que j'ai dans un objet image et de le rendre dans un contexte Core Graphics PDF - se trouve sur un iPhone mais cette question s'applique sûrement également au bureau Quartz. Cette UIImage est une simple image couleur sur blanc à une résolution d'environ 600x800. Si je (dis) le transforme en un fichier PNG, ce fichier ressemble exactement à ce que l'on attend - donc les données sont OK.Le rendu de UIImage/CGImage dans CGPDFContext aboutit à ... blankness!

Voici ce que je fais pour générer le PDF:

NSMutableData * outputData = [[NSMutableData alloc] init]; 
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)outputData); 

CFMutableDictionaryRef attrDictionary = NULL;  
attrDictionary = CFDictionaryCreateMutable(NULL, 0, 
             &kCFTypeDictionaryKeyCallBacks, 
             &kCFTypeDictionaryValueCallBacks); 
CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, @"My Awesome Document"); 
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary); 
CFRelease(dataConsumer); 
CFRelease(attrDictionary); 
CGImageRef pageImage = [myUIImage CGImage]; 
CGPDFContextBeginPage(pdfContext, NULL); 
CGContextDrawImage(pdfContext, CGRectMake(0, 0, [myUIImage size].width, [myUIImage size].height), pageImage); 
CGPDFContextEndPage(pdfContext); 
CGPDFContextClose(pdfContext); 
CGContextRelease(pdfContext); 

Le PDF résultant, qui se termine en outputData, semble comme un fichier PDF valide (ouvre correctement, le titre du document est présent dans les métadonnées), mais il consiste précisément en une page blanche.

Qu'est-ce que je fais mal?

Merci.

MISE À JOUR: Ha! C'était ma faute. Mon code de test pour simplement générer le fichier PNG est passé par un chemin différent pour obtenir les données. Le chemin PDF recevait en effet une image vide.

+0

-vous créer dynamiquement myUIImage ou est une ressource d'image chargée? Si vous dessinez l'image avec du code, pouvez-vous fournir la source? –

Répondre

4

Je l'ai testé votre code et ça a l'air de marcher.
Etes-vous sûr que votre UIImage est valide et non nulle lorsque vous l'insérez dans le contexte?
Ma méthode d'essai charge un .png du faisceau principal et écrit la finale pdf à un fichier dans le dossier d'applications de document:

- (IBAction)outputPDF:(id)sender 
{ 
    NSMutableData* outputData = [[NSMutableData alloc] init]; 
    CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)outputData); 
    CFMutableDictionaryRef attrDictionary = NULL;  
    attrDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 
    CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, @"My Awesome Document"); 
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary); 
    CFRelease(dataConsumer); 
    CFRelease(attrDictionary); 
    UIImage* myUIImage = [UIImage imageNamed:@"tmp.png"]; 
    CGImageRef pageImage = [myUIImage CGImage]; 
    CGPDFContextBeginPage(pdfContext, NULL); 
    CGContextDrawImage(pdfContext, CGRectMake(0, 0, [myUIImage size].width, [myUIImage size].height), pageImage); 
    CGPDFContextEndPage(pdfContext); 
    CGPDFContextClose(pdfContext); 
    CGContextRelease(pdfContext); 
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex:0]; 
    NSString* appFile = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"]; 
    [outputData writeToFile:appFile atomically:YES]; 
} 
+0

Voir la mise à jour à la question d'origine. Je n'ai pas besoin de ce représentant de toute façon :) - Bounty à vous pour les deux en train d'essayer mon code, et pour revenir sur moi plus tard. Merci. –

+0

Merci beaucoup! –

0

Cette question semble avoir été abordé sur les forums iphone dev sdk: PDF creation tutorial

Tout le crédit va à danielb21: voici une réédition de leur méthode CreatePDFFile:

// Our method to create a PDF file natively on the iPhone 
// This method takes two parameters, a CGRect for size and 
// a const char, which will be the name of our pdf file 
void CreatePDFFile (CGRect pageRect, const char *filename) { 

    // This code block sets up our PDF Context so that we can draw to it 
    CGContextRef pdfContext; 
    CFStringRef path; 
    CFURLRef url; 
    CFMutableDictionaryRef myDictionary = NULL; 
    // Create a CFString from the filename we provide to this method when we call it 
    path = CFStringCreateWithCString (NULL, filename, 
             kCFStringEncodingUTF8); 
    // Create a CFURL using the CFString we just defined 
    url = CFURLCreateWithFileSystemPath (NULL, path, 
             kCFURLPOSIXPathStyle, 0); 
    CFRelease (path); 
    // This dictionary contains extra options mostly for 'signing' the PDF 
    myDictionary = CFDictionaryCreateMutable(NULL, 0, 
              &kCFTypeDictionaryKeyCallBacks, 
              &kCFTypeDictionaryValueCallBacks); 
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File")); 
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name")); 
    // Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary 
    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary); 
    // Cleanup our mess 
    CFRelease(myDictionary); 
    CFRelease(url); 
    // Done creating our PDF Context, now it's time to draw to it 

    // Starts our first page 
    CGContextBeginPage (pdfContext, &pageRect); 

    // Draws a black rectangle around the page inset by 50 on all sides 
    CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width - 100, pageRect.size.height - 100)); 

    // This code block will create an image that we then draw to the page 
    const char *picture = "Picture"; 
    CGImageRef image; 
    CGDataProviderRef provider; 
    CFStringRef picturePath; 
    CFURLRef pictureURL; 

    picturePath = CFStringCreateWithCString (NULL, picture, 
             kCFStringEncodingUTF8); 
    pictureURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), picturePath, CFSTR("png"), NULL); 
    CFRelease(picturePath); 
    provider = CGDataProviderCreateWithURL (pictureURL); 
    CFRelease (pictureURL); 
    image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault); 
    CGDataProviderRelease (provider); 
    CGContextDrawImage (pdfContext, CGRectMake(200, 200, 207, 385),image); 
    CGImageRelease (image); 
    // End image code 

    // Adding some text on top of the image we just added 
    CGContextSelectFont (pdfContext, "Helvetica", 16, kCGEncodingMacRoman); 
    CGContextSetTextDrawingMode (pdfContext, kCGTextFill); 
    CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1); 
    const char *text = "Hello World!"; 
    CGContextShowTextAtPoint (pdfContext, 260, 390, text, strlen(text)); 
    // End text 

    // We are done drawing to this page, let's end it 
    // We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage 
    CGContextEndPage (pdfContext); 

    // We are done with our context now, so we release it 
    CGContextRelease (pdfContext); 
}