iOS-PDF-Reader icon indicating copy to clipboard operation
iOS-PDF-Reader copied to clipboard

When using CoreGraphics to draw in background thread memory usage of the app is getting very high until the App eventually crashes

Open customer-glassboxdigital opened this issue 5 years ago • 1 comments

If I call the following function (drawBlackRect) from AppDelegate didFinishLaunchingWithOptions and play with the pdf (zoom in/out, swipe to next page etc ...), the memory usage of the app is getting very high until the app eventually crashes. I also added the same code translated to objective c to PhotoScroller, and the issue doesn't reproduce.

func drawBlackRect() { DispatchQueue.global(qos: .default).asyncAfter(deadline: .now() + .milliseconds(500)) {

               let width = 375;
               let height = 667;
               var imageBuffer = [UInt8](repeating: 0, count: width*height*4)
               imageBuffer.withUnsafeMutableBytes { pointer in
                   
                   guard let context = CGContext(data: pointer.baseAddress,
                                               width: width,
                                               height: height,
                                               bitsPerComponent: 8,
                                               bytesPerRow: width*4,
                                               space: self.colorSpace,
                                               bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue).rawValue)
                   else {
                       return
                   }
                   // Draw a black background
                   let layer = CALayer()
                   layer.backgroundColor = UIColor.black.cgColor;
                   layer.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
                   layer.render(in: context)
                   let _ = context.makeImage();
               
               }
           }
           self.drawBlackRect()
   }

let colorSpace = CGColorSpaceCreateDeviceRGB()