我有一种方法将 PDF 转换为图像(每个页面都转换为 UIImage),并调整每个图像的大小。方法正在以 QOS = 背景调用自定义队列。
当我尝试处理超过 90 页的 PDF 时,出现错误并且我的应用程序崩溃了:
Message from debugger: Terminated due to memory issue
这是我的方法:
func convertPDFToImages(pdfURL: URL) -> ContiguousArray<UIImage>? {
guard let pdfDocument = PDFDocument(url: pdfURL) else {
return nil
}
var images: ContiguousArray<UIImage> = []
autoreleasepool {
for pageNum in 0..<pdfDocument.pageCount {
if let pdfPage = pdfDocument.page(at: pageNum) {
let pdfPageSize = pdfPage.bounds(for: .mediaBox)
let renderer = UIGraphicsImageRenderer(size: pdfPageSize.size)
var image = renderer.image { ctx in
UIColor.white.set()
ctx.fill(pdfPageSize)
ctx.cgContext.translateBy(x: 0.0, y: pdfPageSize.size.height)
ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
pdfPage.draw(with: .mediaBox, to: ctx.cgContext)
}
image = scalePreservingAspectRatio(image, newWidth: newWidth)
images.append(image)
}
}
}
return images
}
func scalePreservingAspectRatio(_ image: UIImage, newWidth: CGFloat) -> UIImage {
let scaleFactor = newWidth / image.size.width
let scaledImageSize = CGSize(
width: newWidth,
height: image.size.height * scaleFactor
)
let renderer = UIGraphicsImageRenderer(
size: scaledImageSize
)
let scaledImage = renderer.image { _ in
image.draw(in: CGRect(
origin: .zero,
size: scaledImageSize
))
}
return scaledImage
}
我尝试过的:
scalePreservingAspectRatio
获取图像 - 仍然是一个错误func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
// Calculate the aspect ratio of the original image
let aspectWidth = targetSize.width / image.size.width
let aspectHeight = targetSize.height / image.size.height
// Use the smaller scale factor to maintain the aspect ratio
let aspectRatio = min(aspectWidth, aspectHeight)
enter code here
// Calculate the new size based on the aspect ratio
let scaledSize = CGSize(width: image.size.width * aspectRatio, height: image.size.height * aspectRatio)
// Use an autorelease pool to help manage memory efficiently
return autoreleasepool {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
let renderer = UIGraphicsImageRenderer(size: scaledSize, format: format)
let resizedImage = renderer.image { _ in
image.draw(in: CGRect(origin: .zero, size: scaledSize))
}
return resizedImage
}
}