让我解释一下我想要构建的内容,因此我为一个包含许多PDF文件的展会做了一个应用程序。我编写了一个加载所有PDF文件的预加载(PDF转换为图像并在将filePath存储到数组后按文件保存)。它工作正常,但一段时间后我得到一个内存问题,因为功能不释放内存。那么有没有办法释放我的记忆。
这是我的功能(方法):
func PDFtoImagesOnePages()
{
let path: String = NSBundle.mainBundle().pathForResource(PDFname, ofType: "pdf")!
let url: NSURL = NSURL.fileURLWithPath(path)
let pdfDoc: CGPDFDocumentRef = CGPDFDocumentCreateWithURL(url)!
let pageCount = CGPDFDocumentGetNumberOfPages(pdfDoc)
for index in 1...pageCount
{
let page = CGPDFDocumentGetPage(pdfDoc, index)
let pageRect = CGPDFPageGetBoxRect(page, .MediaBox)
UIGraphicsBeginImageContext(CGSizeMake(768, 1024*(pageRect.size.height/pageRect.size.width)))
var myContext = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(myContext, UIColor.whiteColor().CGColor)
CGContextFillRect(myContext,pageRect)
CGContextTranslateCTM(myContext, 0.0, pageRect.size.height);
CGContextScaleCTM(myContext, 1.0, -1.0);
CGContextDrawPDFPage(myContext, page);
var img = UIGraphicsGetImageFromCurrentImageContext()
// MARK: - Write image to user path
var documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
documentsPath = documentsPath.stringByAppendingFormat("/\(PDFname)\(index).jpg")
UIGraphicsEndImageContext()
imageData = UIImageJPEGRepresentation(img, 5)
imageData!.writeToFile(documentsPath, atomically: true)
// MARK: - Array imagePath
imagePath.append(documentsPath)
myContext = nil
img = nil
imageData = nil
documentsPath = ""
// MARK: - Counter for UIProgressView
progressPDF = progressPDF + 0.001
progressHolder = progressHolder + 0.001
print("value from progressPDF: \(progressPDF)")
}
}
和启动函数的initViewController:
@IBAction func startRoot(sender: AnyObject) {
startButton.hidden = true
Dispatch.async {
PDF1.delegate = self
PDF1.PDFtoImagesOnePages()
PDF1.delegate = nil
PDF2.delegate = self
PDF2.PDFtoImagesOnePages()
PDF2.delegate = nil
PDF3.delegate = self
PDF3.PDFtoImagesOnePages()
PDF3.delegate = nil
PDF4.delegate = self
PDF4.PDFtoImagesOnePages()
PDF4.delegate = nil
PDF5.delegate = self
PDF5.PDFtoImagesOnePages()
PDF5.delegate = nil
PDF6.delegate = self
PDF6.PDFtoImagesOnePages()
PDF6.delegate = nil
PDF7.delegate = self
PDF7.PDFtoImagesOnePages()
PDF7.delegate = nil
/*
PDF8.delegate = self
PDF8.PDFtoImagesOnePages()
PDF9.delegate = self
PDF9.PDFtoImagesOnePages()
PDF10.delegate = self
PDF10.PDFtoImagesOnePages()
PDF11.delegate = self
PDF11.PDFtoImagesOnePages()
PDF12.delegate = self
PDF12.PDFtoImagesOnePages()
PDF13.delegate = self
PDF13.PDFtoImagesOnePages()
PDF14.delegate = self
PDF14.PDFtoImagesOnePages()
PDF15.delegate = self
PDF15.PDFtoImagesOnePages()
PDF16.delegate = self
PDF16.PDFtoImagesOnePages()
PDF17.delegate = self
PDF17.PDFtoImagesOnePages()
PDF18.delegate = self
PDF18.PDFtoImagesOnePages()
PDF19.delegate = self
PDF19.PDFtoImagesOnePages()
PDF20.delegate = self
PDF20.PDFtoImagesOnePages()
PDF21.delegate = self
PDF21.PDFtoImagesOnePages()
PDF22.delegate = self
PDF22.PDFtoImagesOnePages()
PDF23.delegate = self
PDF23.PDFtoImagesOnePages()
*/
Dispatch.main {
self.textLabel.text = "DONE"
self.performSegueWithIdentifier("startRoot", sender: self)
}
}
答案 0 :(得分:0)
我用“autoreleasepool”修复了问题自我...
func PDFtoImagesOnePages()
{
let path: String = NSBundle.mainBundle().pathForResource(PDFname, ofType: "pdf")!
let url: NSURL = NSURL.fileURLWithPath(path)
let pdfDoc: CGPDFDocumentRef = CGPDFDocumentCreateWithURL(url)!
let pageCount = CGPDFDocumentGetNumberOfPages(pdfDoc)
for index in 1...pageCount
{
autoreleasepool({ () -> () in
let page = CGPDFDocumentGetPage(pdfDoc, index)
let pageRect = CGPDFPageGetBoxRect(page, .MediaBox)
UIGraphicsBeginImageContextWithOptions(pageRect.size, true, 0)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillRect(context,pageRect)
CGContextTranslateCTM(context, 0.0, pageRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawPDFPage(context, page);
let img = UIGraphicsGetImageFromCurrentImageContext()
// MARK: - Write image to user path
var documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
documentsPath = documentsPath.stringByAppendingFormat("/\(PDFname)\(index).jpg")
UIGraphicsEndImageContext()
imageData = UIImageJPEGRepresentation(img, 5)
imageData!.writeToFile(documentsPath, atomically: true)
// MARK: - Array imagePath
imagePath.append(documentsPath)
})
// MARK: - Counter for UIProgressView
progressPDF = progressPDF + 0.001008064
progressHolder = progressHolder + 0.001008064
print("value from progressPDF: \(progressPDF)")
}
}