ios - 无法使用 CGPDFDocumentCreateWithURL 读取 PDF 文档
问题描述
我尝试在 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);
这个片段可能有什么问题?我对 iOS 原生开发和 Objective-C 没有太多经验。
解决方案
首先检查文件是否存在于应用程序包中的给定位置。
强烈推荐使用 URL 相关的 APINSBundle
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];
推荐阅读
- c - 将十六进制类型转换为字符而不更改 C 中的值
- python - python - 如何在python中使用不同符号分割另一个文本之后获取文本?
- java - 如何从java中的数组中获取具有固定字符串的对象?
- javascript - 在 JS 中使用 Fetch API 检索远程 .csv 文件
- python - 将多个值从元组发送到函数
- python-3.x - 以顺序方式提交snakemake作业
- c# - WPF 将 ComboBox ItemSouce 绑定到 ObservableCollection 并将 SelectedItem 绑定到 DataContext 模型属性
- c# - 通过模型的属性更改 TreeViewItem 颜色
- c# - 将 JsonConvertorFactory 添加到 Blazor Web 程序集应用程序
- angular - 如何在下拉选项加载完成后确保下拉模型加载而不破坏角度 9 中代码的异步性质