我在代码中所做的就是迭代我的图像集中的每个图像:
print(UIScreen.main.scale) //3.0
print(UIScreen.main.nativeScale) //3.0
for card in box.sortedCards {
if let image = card.image?.scaledWithMaxWidthOrHeightValue(value: 300) {
print(image.size.width) //300
print(image.size.height) //300
if let page = PDFPage(image: image) {
document.insert(page, at: document.pageCount)
}
}
}但一旦我使用UIActivityViewController预览我的PDFDocument,然后将它分享到我的macbook上,我就会得到以下结果:

它是如何计算的?
通过UIActivityViewController分享到我的苹果电脑上的每张图片都有以下信息:

我需要什么?
我需要计算图像大小,以预览PDFDocument与每页恰好10厘米对10厘米,无论什么ios设备将用于它。
发布于 2021-03-08 20:49:35
它的计算方式
图像的物理大小(以像素为单位)等于图像的逻辑大小乘以图像的比例因子。
Image Size (pixels) = UIImage.size * UIImage.scale如果比例因子为1,则图像的DPI为每英寸72像素。
Image DPI (pixels/inch) = UIImage.scale * 72.0要获取页面大小,请执行以下操作:
Page Size (inches) = Image Size / Image DPI如何获取10x10厘米大小的页面
我不确定您的scaledWithMaxWidthOrHeightValue是如何实现的。为了说明计算过程,我假设您已经有一个size为300x300的UIImage实例。
print(UIScreen.main.scale) //3.0
print(UIScreen.main.nativeScale) //3.0
for card in box.sortedCards {
if let image = card.image?.scaledWithMaxWidthOrHeightValue(value: 300) {
print(image.size.width) //300
print(image.size.height) //300
let defaultDPI = 72.0
let centimetersPerInch = 2.54
let expectedPageSize = 10.0 // centimeters
var scale = 300.0 / defaultDPI * centimetersPerInch / expectedPageSize * image.scale.native
scale += 0.001 // work around accuracy to get exact 10 centimeters
if let cgImage = image.cgImage {
let scaledImage: UIImage = UIImage(cgImage: cgImage, scale: CGFloat(scale), orientation: image.imageOrientation)
if let page = PDFPage(image: scaledImage) {
document.insert(page, at: document.pageCount)
}
}
}
}https://stackoverflow.com/questions/66518398
复制相似问题