title: Why app Terminates due to memory waring when convert pdf page to high quality image in ios, swift in real device
body: <p>I'm trying to get the image(high quality) of each pdf page. I'm using below code running through a for loop until page count and it works.</p>
  
  <pre><code>    guard let document = CGPDFDocument(pdfurl as CFURL) else { return }
      guard let page = document.page(at: i) else { return }
      let dpi: CGFloat = 300.0/72.0
      let pagerect = page.getBoxRect(.mediaBox)
      print(pagebounds)
      print(pagerect)
      let render = UIGraphicsImageRenderer(size: CGSize(width: pagerect.size.width * dpi, height: pagerect.size.height * dpi))
  
      let imagedata = render.jpegData(withCompressionQuality: 0.5, actions: { cnv in
          UIColor.white.set()
          cnv.fill(pagerect)
          cnv.cgContext.translateBy(x: 0.0, y: pagerect.size.height * dpi)
          cnv.cgContext.scaleBy(x: dpi, y: -dpi)
          cnv.cgContext.drawPDFPage(page)
      })
      let image = UIImage(data: imagedata)
  </code></pre>
  
  <p><strong>I'm getting following issues with this ...</strong></p>
  
  <ul>
  <li>sometimes the <code>image</code> is <code>nil</code>.</li>
  <li>When this runs, the usage of <code>memory is very high</code>.</li>
  <li>With the page count(number of pages), usage of memory is very very high, and sometimes it goes to <code>1.4 GB</code> and suddenly it crashes the app with the warning : <code>Terminate due to memory waring</code> . then I tried to run above code inside <code>autoreleasepool</code>. it did work but when the memory usage is more high (when it near to RAM size), again app crashes with above warning.</li>
  </ul>
  
  <p>How can I avoid this memory warning and get the quality image form pdf page. hope any help. have a nice day.</p>