在启动循环后由于内存问题而终止

让我解释一下我想要构build的东西,所以我为一个公平的PDF文件做一个应用程序。 我编写了一个预加载所有的PDF文件(PDF格式转换为图像,并在将数据存储到文件path后保存)。 它工作正常,但过了一段时间我得到一个内存问题,因为function不释放内存。 那么是否有办法释放我的记忆。

这里是我的function(方法):

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) } } 

我用“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)") } }