在塞拉利昂有一个问题,图片在使用PDFKit添加到PDF文档时看到空白。您需要使用低级Quart 2D编程指南apis来获取正确的pdf。
Apple Documentation因为它在这里。
示例代码 -
CFStringRef path = CFStringCreateWithCString (NULL, filename,kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease (path);
CFMutableDictionaryRef myDictionary = myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("Some property string"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("Some property string"));
CFRelease(myDictionary);
pageDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDataRef boxData = CFDataCreate(NULL,(const UInt8 *)&pageRect, sizeof (CGRect));
CFDictionarySetValue(pageDictionary, kCGPDFContextMediaBox, boxData);
CGContextRef pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL (url);
CFRelease(url);
ViewController* currentView = [story instantiateControllerWithIdentifier:viewName];
NSImage *currentImage = [self imageRepresentation:currentView.view];
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)[currentImage TIFFRepresentation], NULL);
CGImageRef maskRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CGPDFContextBeginPage (pdfContext, pageDictionary);
CGContextDrawImage(pdfContext, pageRect, maskRef);
CGPDFContextEndPage (pdfContext);
CGContextRelease (pdfContext);
CFRelease(pageDictionary);
CGPDFDocumentRelease (pdfDocument);
的代码是正确的。我还没有升级到Sierra。应该管用。 – Tushar
MacOS 10.12 PDFKit Sierra有许多问题和bug,但是这个代码在MacOS 10.12 Sierra上适用于我。 – danielv
上面的代码工作和创建pdf文件..但图像不加起来。文字和其他东西也在起作用。 –