Skip to content

Fix heic color space handling on iOS#358

Open
anthonyVuAtPropertyMe wants to merge 2 commits into
fluttercandies:mainfrom
anthonyVuAtPropertyMe:fix-heic-color-space-handling
Open

Fix heic color space handling on iOS#358
anthonyVuAtPropertyMe wants to merge 2 commits into
fluttercandies:mainfrom
anthonyVuAtPropertyMe:fix-heic-color-space-handling

Conversation

@anthonyVuAtPropertyMe
Copy link
Copy Markdown

Fix iOS HEIC encoding with proper color space handling and error validation

Problem

JPEG to HEIC conversion on iOS was silently failing. The compression would appear to succeed, but the HEIC file was never actually written to disk. Additionally, the code lacked proper error handling and memory management for color space resources.

Root Cause

The issue was a subtle Objective-C compiler optimization. In CompressFileHandler.m, the return value of writeToURL:atomically: was ignored, causing the compiler to optimize away the file write call entirely as "dead code" since the result didn't affect anything downstream. This meant the [data writeToURL:...] call was never executed in the compiled binary.

Changes

CompressFileHandler.m

  • Capture return value: Changed from ignoring the return value to explicitly capturing it in a BOOL success variable
  • Add nil validation: Check if compressed data is nil before attempting to write, preventing crashes
  • Proper error feedback: Return nil on write failure instead of always returning the path, giving accurate feedback to callers

CompressHandler.m

  • Color space handling: Get color space from the CIImage or create sRGB fallback if missing
  • Memory management: Properly release allocated color space only when newly created, preventing memory leaks
  • Error logging: Added detailed error logging when HEIC encoding fails to aid debugging

Impact

✅ JPEG to HEIC conversion now successfully creates files on iOS
✅ Proper error reporting to callers about operation success/failure
✅ Prevents potential memory leaks from color space resources
✅ Better debugging with error messages

@anthonyVuAtPropertyMe anthonyVuAtPropertyMe changed the title Fix heic color space handling Fix heic color space handling on iOS May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant