我在iOS 11.3上生成pdf时遇到错误。它适用于iOS 11.2。
CGDataConsumerCreateWithFilename: failed to open `/note.pdf' for writing: Operation not permitted.
deflateEnd: error -3: (null).
Pdf生成良好,但当我尝试访问它以进行上传时,会产生错误。代码相同:
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL:URL = documentsURL.appendingPathComponent("note.pdf")
do {
try FileManager.default.createDirectory(atPath: fileURL.path, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
UIGraphicsBeginPDFContextToFile((fileURL.path as NSString).lastPathComponent as String?, CGRectZero, nil);
最后一行产生错误。
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
您要将文件保存在根目录中,不能这样做,请尝试此操作
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL:URL = documentsURL.appendingPathComponent("note.pdf")
do {
try FileManager.default.createDirectory(atPath: fileURL.path, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
UIGraphicsBeginPDFContextToFile(fileURL.appendingPathComponent("note.pdf").path, CGRect.zero, nil);
哪个会得到,根据你在做什么,你将创建一个 文件夹
note.pdf
然后将文件note.pdf
保存在其中