新しく受信した PDF ドキュメントのサムネイルを Documents-Directory にレンダリングします。
私は次のコードを使用しています:
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]);
}
コンソールにエラーはありませんが、PNG はドキュメント ディレクトリに保存されません。ブール
writtenBool
は「0」です。これは、書き込みアクションが成功しなかったことを意味します。どうしてか分かりません。pngPath のパスをコンソールにも書き込み、パスは正しいです。ターミナルを開いて書くと
open <<copyed path from console>>
ファインダーで正しいパスを開きます。
これが機能しない原因は何ですか? 私はAPIを見ましたが、ないようです
error:
UIImagePNGRepresentation の場合: writeToFile:
ご協力いただきありがとうございます!