Home > Blockchain >  Saving images more efficiently in favor of minimum memory use
Saving images more efficiently in favor of minimum memory use

Time:10-23

I'm observing the Pasteboard for changes, that include images. The amount of memory I can use is limited, so most images make the program result in a jetsam crash due to their size. Is it possible to store them more efficiently?

Currently I'm using this code to store them:

for (UIImage* image in [_pasteboard images]) {
    [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
}

CodePudding user response:

All of those images are staying in memory in this loop, which is causing your memory use problem. You can help by adding an autorelease pool inside the loop:

for (UIImage* image in [_pasteboard images]) {
    @autoreleasepool {
        [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
    }
}

That will cause each image's memory to be reclaimed at the end of the current pass through the loop.

I'm removing the "core-data" tag since this question is not about Core Data.

CodePudding user response:

I solved it by making a different process with more memory do the work. The original process only sends a notification now.

  • Related