objective-cmacosnsdatacgimageref

Converting CGImageRef to NSData increase app memory usage in Mac app


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


Solution

  • 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