如何使用pdfkit通过相机拍摄的图像创建pdf

时间:2018-02-04 14:44:13

标签: ios swift uiimage uiimagepickercontroller pdfkit

您好我是pdfkit和iOS应用程序的新用户,我想用设备拍照,然后将图像转换为pdf。用相机拍照并将其保存在相机胶卷上工作完全正常,问题是当我想从该图像创建pdf时它需要图像的名称,我怎么能找到它? 另一个问题是可以用pdfkit实现任何pdf编辑器来编辑图像吗? 感谢任何帮助。以下是我的imagePickerController代码。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

    if (Mylibrary)
    {

    }
    else
    {
        UIImageWriteToSavedPhotosAlbum(pickedImage!, nil, nil, nil);
        let image = UIImage(named: "????") //the problem is here
        let newPage = PDFPage(image: image!)
    }

    picker.dismiss(animated: true, completion: nil)




}

1 个答案:

答案 0 :(得分:1)

由于图像已存储在pickedImage变量中,因此无需实例化新图像。您只需要使用PDFPage初始化新的pickedImage对象。使用ImageKit编辑图像,然后再使用它初始化新的PDFPage对象。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    guard let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }

    // Adds pickedImage to the user’s Camera Roll album.
    UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil)
    // Creates a new PDFPage object and initializes it with pickedImage.
    let newPage = PDFPage(image: pickedImage)

    dismiss(animated: true, completion: nil)
}