【问题标题】:Can't read PDF document using CGPDFDocumentCreateWithURL无法使用 CGPDFDocumentCreateWithURL 读取 PDF 文档
【发布时间】:2020-08-04 17:40:32
【问题描述】:

我尝试在 iOS 上加载 PDF 失败,但在调试代码时文档显示“0x0”作为值。

NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSString *pdfPath = [appFolderPath stringByAppendingString:@"/Data/Raw/test.pdf"];
    
CFStringRef cfsPath = (__bridge CFStringRef)pdfPath;

CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cfsPath, kCFURLPOSIXPathStyle, 0);
    
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);

这个 sn-p 有什么问题?我在 iOS 原生开发和 Objective-C 方面没有太多经验。

【问题讨论】:

  • 嗨,Edu - 为什么不用NSURL 代替所有那些弦乐体操呢?用它替换你的 URL,生活会容易得多。至于错误,我怀疑你的路径可能是错误的,或者那里可能没有文档。

标签: ios objective-c xcode ios-pdfkit


【解决方案1】:

首先检查文件是否存在于应用程序包中的给定位置。

强烈推荐使用NSBundle的URL相关API

NSURL *appFolderURL = [[NSBundle mainBundle] resourceURL];
NSURL *pdfURL = [appFolderURL URLByAppendingPathComponent:@"Data/Raw/test.pdf"];

并使用更方便的PDFDocument PDFKit 类而不是 CoreFoundation API

@import PDFKit;

PDFDocument *document = [[PDFDocument alloc] initWithURL:pdfURL];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多