2

i render Thumbnails of newly recieved PDF Document to the Documents-Directory.

I'm using the following code:

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("thedoc.pdf"), NULL, NULL);
        CGPDFDocumentRef bookPDF = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
        UIGraphicsBeginImageContext(CGSizeMake(100, 130));
        NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(bookPDF);
        CGContextRef context = UIGraphicsGetCurrentContext();

        for(int i = 0; i < totalNum; i++ ) {
            CGRect arect = CGRectMake(0,0,200,282);
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, 0.0, 141);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextSetGrayFillColor(context, 1.0, 1.0);
            CGContextFillRect(context, arect);
            // Grab the first PDF page
            CGPDFPageRef page = CGPDFDocumentGetPage(bookPDF, i + 1);
            CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, arect, 0, true);
            // And apply the transform.
            CGContextConcatCTM(context, pdfTransform);
            CGContextDrawPDFPage(context, page);
            // Create the new UIImage from the context
            UIImage* thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
            if (thumbnailImage == nil) {
                NSLog(@"ERROR during creation of PNG");
            }
            // SAVE THE IMAGE TO DOCUMENTS-DIRECTORY
            NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/thumbPage%i.png",theLocalFolder ,i]];
            // Write image to PNG
            BOOL writtenBool = [UIImagePNGRepresentation(thumbnailImage) writeToFile:pngPath atomically:YES];
            // END SAVE THE IMAGE TO DOCUMENT-DIRECTORY
            CGContextRestoreGState(context);
            NSLog(@"Rendering PDF-Thumbnail %i (%i): %@", i, writtenBool, [NSString stringWithFormat:@"%@/thumbPage%i.png",theLocalFolder ,i]);
        }

There is no error in Console, but the PNGs are not stored to the documents-Directory. The BOOL

writtenBool

is "0", what means that the write-action was not succuessful. I don't know why. I write the path in pngPath also to the console and the path is correct. If i open a terminal and write

open <<copyed path from console>>

it opens the correct path in finder.

What could cause this not to work? I had a look at the api but there seems to be no

error:

for UIImagePNGRepresentation: writeToFile:

Thanks for your help!

MadMaxAPP
  • 909
  • 1
  • 11
  • 37

1 Answers1

6

UIImagePNGRepresentation(thumbnailImage) return a NSData object

for NSData object, you can use these methods:

writeToFile:atomically:,

writeToFile:options:error:,

writeToURL:atomically:,

writeToURL:options:error:,

so, you can try use code like BOOL writtenBool = [UIImagePNGRepresentation(thumbnailImage) writeToFile:pngPath options:NSDataWritingAtomic error:&error]; to see what happened.

sycx
  • 851
  • 6
  • 8