我有一个捕获图像并将其转换为 pdf 的应用程序。然后显示在 PDFView 中。但是,PDF 不适合我的 PDFView 的尺寸。如何缩放 pdf 以适应 PDFView?
当从 imagePickerController 中选择图像时,将执行以下函数:
// OUTLETS
@IBOutlet weak var myPDFView: PDFView!
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("image selected ...")
// Get picked image info dictionary
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
// Add captured and selected image to your Photo Library
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
// Create PDFDocument object and display in PDFView
let document = createPDFDataFromImage(image: image)
myPDFView.document = document
myPDFView.scaleFactorForSizeToFit
// Dimiss imagePicker
dismiss(animated: true, completion: nil)
}
func createPDFDataFromImage(image: UIImage) -> PDFDocument{
let document = PDFDocument.init()
let imagePDF = PDFPage.init(image: image)
document.insert(imagePDF!, at: 0)
return document
}