崩溃whille将NSURL转换为CFURL

Ani*_*eja 5 nsurl ios cgcontextref swift

我想在文档目录中创建pdf,并希望提供页码,所以我需要CGPDFDocumentRef对象.

let fileName: NSString = "test.pdf"

let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

let documentDirectory: AnyObject = path.objectAtIndex(0)

let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName as String)

UIGraphicsBeginPDFContextToFile(pdfPathWithFileName as String, CGRectZero, nil)

let ref : CGContextRef = UIGraphicsGetCurrentContext()!

let localUrl = NSURL.fileURLWithPath(pdfPathWithFileName)
Run Code Online (Sandbox Code Playgroud)

我已经在url中转换了文件路径,但这下面的行会导致崩溃,我不知道为什么......?

let pdfDocumentRef: CGPDFDocumentRef = CGPDFDocumentCreateWithURL(localUrl as CFURLRef)!
Run Code Online (Sandbox Code Playgroud)

Dha*_*esh 0

这是因为您的test.pdf文档目录文件夹中不存在。