Swift UIView、PDF化、複数枚、保存

UIView、PDF化、複数枚、保存

UIView、PDF化、保存
の複数枚版。

import UIKit

class APCDCalendar {
    func export() -> URL? {
        var result: URL? = nil
        
        let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 1366.0, height: 1024.0))
        let templateView = UIImageView(image: UIImage(named: "aptemplate"))
        templateView.frame = CGRect(x: 0.0, y: 0.0, width: 1366.0, height: 1024.0)
        templateView.contentMode = .scaleAspectFit
        view.addSubview(templateView)
        
        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, view.bounds, nil)
        guard let pdfContext = UIGraphicsGetCurrentContext() else { return result}
        UIGraphicsBeginPDFPage()
        view.layer.render(in: pdfContext)
        UIGraphicsBeginPDFPage()
        view.layer.render(in: pdfContext)
        UIGraphicsEndPDFContext()
            
        if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
            let documentsFileName = documentDirectories + "/" + "APCDigital_ec.pdf"
            pdfData.write(toFile: documentsFileName, atomically: true)
            result = URL(fileURLWithPath: documentsFileName)
        }
        return result
    }
}

UIGraphicsBeginPDFPage、UIView.layer.renderを繰り返せばいい。

Swift

Posted by shi-n