[英]How to fit a PDF from UIImage in PDFView in swift?
我有一個捕獲圖像並將其轉換為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
}
您可以將autoScales設置為true
這是我的代碼
pdfView.document = createPDF(image: #imageLiteral(resourceName: "Image-1"))
pdfView.minScaleFactor = 0.1
pdfView.maxScaleFactor = 5
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.displayDirection = .vertical
然后從這樣的圖像創建PDFPage:
func createPDF(image: UIImage) -> PDFDocument {
let document = PDFDocument()
guard let cgImage = image.cgImage else { return document }
let image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .downMirrored)
guard let imagePDF = PDFPage(image: image) else { return document }
document.insert(imagePDF, at: document.pageCount)
return document
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.