我正试着用swift生成一个pdf。下面的代码编译时没有错误,但是永远不会创建"xp.pdf“文档。非常感谢您的帮助。
import UIKit
import Foundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func generatePDF(sender: AnyObject) {
let pageSize:CGSize = CGSizeMake (850, 1100)
let fileName: NSString = "xp.pdf"
let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory: AnyObject = path.objectAtIndex(0)
let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName)
generatePDFs(pdfPathWithFileName)
}
func generatePDFs(filePath: String) {
UIGraphicsBeginPDFContextToFile(filePath, CGRectZero, nil)
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 850, 1100), nil)
drawBackground()
UIGraphicsEndPDFContext()
}
func drawBackground () {
let context:CGContextRef = UIGraphicsGetCurrentContext()
let rect:CGRect = CGRectMake(0, 0, 850, 1100)
CGContextSetFillColorWithColor(context, UIColor.greenColor().CGColor)
CGContextFillRect(context, rect)
}发布于 2017-10-20 01:42:33
你这里有pdfkit吗?据我所知,PSPDFKit运行得相当好。您只需从pspdfkit.com下载它,然后将PSPDFKit.framework拖放到应用程序目标的“嵌入式二进制文件”部分。你确实需要为它付钱,所以就是这样。我认为它有一个免费的试用版,所以你可以试一试,看看这是不是你需要的。
发布于 2016-04-12 01:43:10
对于Xcode 7 (swift 2.0),我必须进行以下更改才能使其正常工作:
替换:
let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName)通过以下方式:
let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName as String)并解开UIGraphicsGetCurrentContext()
let context:CGContextRef = UIGraphicsGetCurrentContext()!您还可以删除:
let pageSize:CGSize = CGSizeMake (850, 1100)因为您从未使用过pageSize
https://stackoverflow.com/questions/27050493
复制相似问题