-1

UIGraphicsBeginPDFContextToFileとUIGraphicsBeginPDFPageを使用して作成し、テキストと画像を描画する大きなhqPDFファイルにサムネイルを埋め込む必要があります。

ページの親指を埋め込む方法を知っている人はいますか?

チャオ、アルノ

4

1 に答える 1

0

PDF からサムネイルを作成する場合は、以下のコードを使用できます。このコードはそれをディスクに書き込みます。メソッドが画像を返すように変更するのは簡単です。

- (void)createPDFThumbnailForFile:(NSString *)theFilename {
    if (!theFilename) {return;}
    @try {
        NSString *path = [FileInfo fullPathForFile:theFilename];
        NSURL *pdfFileUrl = [NSURL fileURLWithPath:path];
        CFURLRef pdfFileRef = (__bridge CFURLRef) pdfFileUrl;
        CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfFileRef);
        CGPDFPageRef page;
        CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
        UIGraphicsBeginImageContext(aRect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        UIImage *thumbnailImage;
    //    NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);
        //we only want the first page
        for (int i = 0; i < 1; i++) {
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, 0.0, aRect.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextSetGrayFillColor(context, 1.0, 1.0);
            CGContextFillRect(context, aRect);
            // Grab the first PDF page
            page = CGPDFDocumentGetPage(pdf, 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
            thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
            CGContextRestoreGState(context);
        }
        CGPDFDocumentRelease(pdf);
        NSString *pngPath = [path stringByReplacingOccurrencesOfString:@".pdf" withString:@".png"];
    //    [@"test" writeToFile:pngPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        [UIImagePNGRepresentation(thumbnailImage) writeToFile:pngPath atomically:YES];
    }
    @catch (NSException *exception) {
        DebugLog(@"Could not write thumbnail to : %@ /n --> %@", theFileToSave, exception.description);
    }

}
于 2013-03-19T22:03:28.580 に答える