我想将UIImage数组转换为pdf文件.PDF文件页面大小应为A4.我try 了PDFKIT,但结果不合适.

What I want:

  1. A4页大小
  2. 图像应居中并在页面上zoom (数组图像尺寸不相同)

What I tried:

    func makePDF(images: [UIImage])-> PDFDocument? {
    let pdfDoc = PDFDocument()
    
    for (index,image) in images.enumerated() {
        let resizedImage = resizeImage(image: image, targetSize: PDFPageSize.A4)
        guard let cgImage = resizedImage?.cgImage else { return pdfDoc }
        let uiimage = UIImage(cgImage: cgImage, scale: image.scale, orientation: .up)
        if let pdfPage = PDFPage(image: uiimage) {
            let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
            pdfPage.setBounds(page, for: PDFDisplayBox.mediaBox)
            pdfDoc.insert(pdfPage, at: index)
        }
        
    }
    return pdfDoc
}

func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
    let size = image.size
    let widthRatio  = targetSize.width  / size.width
    let heightRatio = targetSize.height / size.height
    var newSize: CGSize
    if(widthRatio > heightRatio) {
        newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
    } else {
        newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
    }
    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

My Output is:

  1. 图像丢失
  2. 图像裁剪
  3. 不在中间位置

My Output

My Desire Output is:

My Desire Output

我该怎么做?

推荐答案

首先,你必须理解为什么你看到你所看到的,这在一定程度上解释了:

PDFKit PDFView coordinate system add UIImage to PDF Center iOS Swift

因为PDF的来源是左下角,当你添加一些东西时,它会被添加到页面的底部,这就是为什么你的图片位于底部.

要解决此问题,您可能需要执行以下操作:

  1. 你的graphics context应该是PDF页面的大小,而不是图片的大小
  2. 您可以在图形上下文中居中显示图像
  3. 将此图像添加到您的pdf页面

以下是我将对你的调整大小功能所做的修改:

func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
    let size = image.size
    let widthRatio  = targetSize.width  / size.width
    let heightRatio = targetSize.height / size.height
    var newSize: CGSize
    
    if(widthRatio > heightRatio) {
        newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
    } else {
        newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
    }
    
    // Calculate the centerX and centerY
    let centerX = (targetSize.width - newSize.width) / 2.0
    let centerY = (targetSize.height - newSize.height) / 2.0
    
    // The rect of the image should be based on the center calculations
    let rect = CGRect(x: centerX,
                      y: centerY,
                      width: newSize.width,
                      height: newSize.height)
    
    // The graphics context should be created with the page dimensions
    UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
    
    // The rest remains the same
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

我相信你会在PDF页面中获得所需的图像输出:

UIImage added to PDFPage PDFKit centered iOS Swift

Insert UIImage in PDF using PDFKit centered iOS Swift

Ios相关问答推荐

Firebase SDK 10+—无法安装软件包

SWIFT根据地球坐标修改 node 旋转

使用SWIFT传输表示时的条件文件表示格式

如何让3张组合的`SF Symbol`图片围绕一个特定点旋转?

Swift导航远离视频导致崩溃

objc[25442]:Swift类的扩展和类别不允许有+load方法

使用 Objective-C 创建 iOS 框架

如何仅更改一个视图的导航标题 colored颜色 ?

SwiftUI 中的偏移量和按钮有问题吗?

我可以使用同一个 Firebase 应用将多个 Flutter 应用添加到一个 Firebase 数据库吗?

向左或向右移动时如何水平翻转sprite?

构建失败:ld:重复符号 _OBJC_CLASS_$_Algebra5FirstViewController

UITextView 从文本的底部或中间开始

iOS 7 圆形框架按钮

核心数据 - 无法在路径加载优化模型

无法加载从带有标识符的包中的笔尖引用的图像

判断从 JSON 字符串返回的 Objective-C 中的空值

如何在 iOS 中获取正在运行的应用程序的名称

我的应用程序因使用广告支持框架而被拒绝.哪个图书馆负责?

为什么我需要@1x、@2x 和@3x iOS 图像?