应用程序中有一个Web视图,该Web视图顶部有一个“保存”按钮。
单击“保存”按钮后,currentURL将被保存到另一个ViewController上的TableView中。一切正常。
现在,我还要将网页另存为PDF到相同的TableView中,并可以在单击后将其打开。我怎样才能做到这一点?
以下是用于保存currenturl的代码。 savedURLs是将所有URL保存在另一个ViewController上的数组。
import UIKit
class BrowserViewController: UIViewController {
@IBOutlet weak var browser: UIWebView!
@IBAction func saveButtonPressed(sender: AnyObject) {
if let currentURL = browser.request?.URL?.absoluteString {
savedURLs.append(currentURL)
NSUserDefaults.standardUserDefaults().setObject(currentURL, forKey: "currenturl")
NSUserDefaults.standardUserDefaults().setObject(savedURLs, forKey: "savedURLs")
self.performSegueWithIdentifier("toSavingVC", sender: self)
}
}
最佳答案
这是用于将webPage转换为图像的Swift代码,然后可以将其转换为PDF:
let sizevid = CGSizeMake(1024, 1100)
UIGraphicsBeginImageContext(sizevid);
webview.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let viewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
var imageData = NSData()
imageData = UIImagePNGRepresentation(viewImage)!
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let pathFolder = String(String(format:"%@", "new.png"))
let defaultDBPathURL = NSURL(fileURLWithPath: documentsPath).URLByAppendingPathComponent(pathFolder)
let defaultDBPath = "\(defaultDBPathURL)"
imageData.writeToFile(defaultDBPath, atomically: true)
// After converting it in to an image you can easily convert it as pdf.