Alua-Kinzhebayeva / iOS-PDF-Reader

PDF Reader for iOS written in Swift
MIT License
532 stars 150 forks source link

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

Open customer-glassboxdigital opened 4 years ago

customer-glassboxdigital commented 4 years ago

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()
   }
customer-glassboxdigital commented 4 years ago

let colorSpace = CGColorSpaceCreateDeviceRGB()