Short version: iOS's UIImageJPEGRepresentation()
crashes on large images. I'm trying to use & modify libjpeg to respect the max_memory_to_use
field, which it's ignoring.
Long version: I'm writing an iOS app which crashes when converting a large image to JPEG after prolonged usage reduces available memory (a trickling leak involving quirks of @autoreleasepool{}
, but we're addressing that separately). For images captured by the device camera (normal use, actual size) UIImageJPEGRepresentation()
can require up to 200MB (!), crashing if not available. This is a problem with UIImageJPEGRepresentation()
which a web search shows goes back for years and seems unsolved; filing a tech support request with Apple elicits "file a bug report" which doesn't solve my immediate customer needs.
To resolve this, I'm bypassing UIImageJPEGRepresentation()
by using libjpeg (http://www.ijg.org) and digging into its operation, which shows exactly the same problem (presumably Apple uses it in iOS). libjpeg does provide a means to specify maximum memory usage via the parameter max_memory_to_use
a la:
struct jpeg_compress_struct cinco;
cinfo.mem->max_memory_to_use = 10*1024*1024;
which would be used by the libjpeg function jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed, long max_bytes_needed, long already_allocated)
(in jmemnobs.c) but, in the standard implementation, is completely ignored (comment even says Here we always say, "we got all you want bud!"
). Blender has altered the function (http://download.blender.org/source/chest/blender_2.03_tree/jpeg/jmemmac.c) to respect the parameter, but seems I'm missing something to make it work in my app or it's just being ignored anyway elsewhere.
So: how does one modify jmemnobs.c in libjpeg to actually & seriously respect memory limitations, rather than jokingly ignore them?