2

I am creating a mac app in which I am converting the CGImageRef to NSData and then Base64 and sending on a server.

This thing happens in Loop, and every time image converted in NSData app memory usage increase.

Here is my code

CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage: screenShot];


NSData *imageData = [bitmapRep representationUsingType:NSPNGFileType properties:nil];;
NSString *base64String = [imageData base64EncodedStringWithOptions:0];

Please let me know if I am doing any mistake, Thank you

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56

1 Answers1

3

What about CGImageRelease(screenShot)? I think it should help. Call it when you're done. Seems like you're not releasing the memory that you have received with CGWindowListCreateImage

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • 1
    I'll add this: Old "stuff", struct like usually with a `Ref` suffix need to be manually release (no ARC). Usually, each have a specific Release method, `CGImageRelease()` in your case, that can be found on the the same level of the documentation: https://developer.apple.com/documentation/coregraphics/cgimage?language=objc – Larme Jan 03 '19 at 11:12