CGPDFDocumentCreateWithURL失败(NSURL与CFURLRef的免费桥接) [英] CGPDFDocumentCreateWithURL fails (Toll-Free bridging of NSURL to CFURLRef)
问题描述
我有以下功能:
- (void)loadPdfFromPath:(NSString*)path
{
NSURL *pathUrl = [NSURL URLWithString:path];
_pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef)pathUrl);
}
我从文档中认为这是可行的,因为您可以从 NSURL *
到 CFURLRef
通过免费桥接。但是,此函数失败,并且我在日志中得到以下输出:
Which from the documentation I'm lead to believe will work because you can case from an NSURL*
to a CFURLRef
via Toll-Free Bridging. However, this function fails, and I get the following output in the log:
CFURLCreateDataAndPropertiesFromResource:错误代码-15。
CFURLCreateDataAndPropertiesFromResource: error code -15.
NB:-15 = kCFURLImproperArgumentsError
NB: -15 = kCFURLImproperArgumentsError
但是,如果我创建一个实际的 CFURLRef
,它就可以正常工作:
However, if I create an actual CFURLRef
, it works absolutely fine:
- (void)loadPdfFromPath:(NSString*)path
{
CGPDFDocumentRelease(_pdfDocument);
CFStringRef cgPath = CFStringCreateWithCString(NULL, [path UTF8String], kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cgPath, kCFURLPOSIXPathStyle, 0);
_pdfDocument = CGPDFDocumentCreateWithURL(url);
CFRelease(url);
CFRelease(cgPath)
}
我缺少什么?我很乐意将第二个函数保留在代码中,但我想知道为什么第一个函数会失败。
What am I missing? I'm happy to keep the second function in my code, but I'd rather know why the first one is failing.
推荐答案
要将文件系统路径转换为URL,请使用
To convert a file system path to a URL, use
NSURL *pathUrl = [NSURL fileURLWithPath:path];
URLWithString
需要符合RFC 2396的URL字符串例如 http:// ...或 file:/// ...,包括方案等。
URLWithString
expects a RFC 2396 conforming URL string like "http://..." or "file:///...", including the "scheme" etc.
已添加: 实际上, NSURL * pathUrl = [NSURL URLWithString:path];
返回一个有效的对象,因此它似乎可以正常工作,但是从此URL(例如 dataWithContentsOfURL
)读取失败。
Added: Actually NSURL *pathUrl = [NSURL URLWithString:path];
returns a valid object, so it seems to work, but reading from this URL (e.g. dataWithContentsOfURL
) fails.
这篇关于CGPDFDocumentCreateWithURL失败(NSURL与CFURLRef的免费桥接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!