The problem is this:
UIGraphicsGetImageFromCurrentImageContext()
returns an autoreleased UIImage
. The autorelease pool holds on to this image until your code returns control to the runloop, which you do not do for a long time. To solve this problem, you would have to create and drain a fresh autorelease pool on every iteration (or every few iterations) of your while
loop.