如何将ImageView转换为PDF文件

时间:2018-03-24 09:56:47

标签: ios swift swift3

我有两个StoryBoards包含TextFields和两个Buttons

第二个StoryBoard从第一个ImageView收到StoryBoard

我想在第二个TextField的{​​{1}}上的ImageView中制作文字,然后将其转换为PDF文件。

我在网站上发现了这个代码,并尝试使用它,但我真的不知道如何使用它,我不能这样做。有人能帮助我吗?

StoryBoard

/

import UIKit
import PDFKit

class ViewController: UIViewController {

    @IBOutlet weak var textview: UITextField!
    @IBOutlet weak var imageview1: UIImageView!
    let PdfView = pdfViewController()
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // Button To send Data To Another view
    @IBAction func aa(_ sender: Any)
    {
        let label = UILabel(frame: CGRect(x: 20, y: 20, width: 80, height: 30))
        label.text = "Hello ...!"
        label.font = UIFont.systemFont(ofSize: 20)
        label.textColor = .black
        view.addSubview(label)

        sample1(label: label)
        //sample2(label: label)
        //sample3(label: label)
        //sample4(label: label)

        PdfView.imageview.clipsToBounds = true
        view.addSubview(PdfView.imageview)
    }

    func sample1(label: UILabel) {
        PdfView.imageview.contentMode = .scaleAspectFit
        PdfView.imageview.image = UIImage(named: "AR1-1")?.with(view: label) { (parentSize, viewToAdd) in
            print("parentSize: \(parentSize)")
            viewToAdd.font = UIFont.systemFont(ofSize: 40)
            viewToAdd.textColor = .yellow
            viewToAdd.bounds = CGRect(x: 40, y: 40, width: 200, height: 40)
        }
    }

    // Button To Move To Another View
    @IBAction func ww(_ sender: Any)
    {

    }

}


extension UIView {

    func copyObject<T: UIView> () -> T? {
        let archivedData = NSKeyedArchiver.archivedData(withRootObject: self)
        return NSKeyedUnarchiver.unarchiveObject(with: archivedData) as? T
    }
}
extension UIImage {

    typealias EditSubviewClosure<T: UIView> = (_ parentSize: CGSize, _ viewToAdd: T)->()

    func with<T: UIView>(view: T, editSubviewClosure: EditSubviewClosure<T>) -> UIImage {

        if let copiedView = view.copyObject() as? T {
            UIGraphicsBeginImageContext(size)

            let basicSize = CGRect(origin: .zero, size: size)
            draw(in: basicSize)
            editSubviewClosure(size, copiedView)
            copiedView.draw(basicSize)

            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage!
        }
        return self

    }
}

extension UIImageView {

    enum ImageAddingMode {
        case changeOriginalImage
        case addSubview
        case addCopiedSubview
    }

    func drawOnCurrentImage<T: UIView>(view: T, mode: ImageAddingMode, editSubviewClosure: @escaping UIImage.EditSubviewClosure<T>) {

        guard let image = image else {
            return
        }

        let addSubView: (T) -> () = { view in
            editSubviewClosure(self.frame.size, view)
            self.addSubview(view)
        }

        switch mode {
        case .changeOriginalImage:
            self.image = image.with(view: view, editSubviewClosure: editSubviewClosure)

        case .addSubview:
            addSubView(view)

        case .addCopiedSubview:
            if let copiedView = view.copyObject() as? T {
                addSubView(copiedView)
            }
        }
    }
}

0 个答案:

没有答案