Skip to content

fix(ios): prevent crash when printing images without internet connection#229

Merged
tr3v3r merged 3 commits into
tr3v3r:mainfrom
tolypash:fix/ios-image-print-crash
Jun 27, 2025
Merged

fix(ios): prevent crash when printing images without internet connection#229
tr3v3r merged 3 commits into
tr3v3r:mainfrom
tolypash:fix/ios-image-print-crash

Conversation

@tolypash

Copy link
Copy Markdown
Contributor

Description

Fix crash when printing images without internet connection on iOS.

Problem

The app crashes with UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={472, 0} when trying to print images while offline. This happens because:

  1. Network requests fail and return nil data
  2. Images are created with invalid dimensions (e.g., width=472, height=0)
  3. UIGraphicsBeginImageContextWithOptions crashes when called with invalid dimensions

Solution

Added comprehensive validation in the iOS image processing pipeline:

  • ImageManager.m: Added checks for nil data, invalid image dimensions, and prevent division by zero
  • ThePrinter.m: Added validation at each step of image processing and return EPOS2_ERR_PARAM instead of crashing

Changes

  • ✅ Validate image data before creating UIImage
  • ✅ Check image dimensions before size calculations
  • ✅ Validate calculated sizes before graphics context creation
  • ✅ Return proper error codes instead of crashing

Testing

  • TypeScript type checking passes
  • ESLint passes
  • Changes are in native iOS code only (no JavaScript changes)

Breaking Changes

None. This is a bug fix that improves error handling.

Checklist

  • Code follows the project's coding standards
  • Changes are focused and related to the issue
  • Commit message follows conventional commits format
  • No breaking changes introduced

- Add validation checks in ImageManager.m to handle nil data and invalid image dimensions
- Add error handling in ThePrinter.m to return EPOS2_ERR_PARAM instead of crashing
- Prevent UIGraphicsBeginImageContext crash with invalid size parameters
- Fix division by zero in getImageCGSize when image width is 0

Fixes crash: UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={472, 0}
Comment thread package/ios/ThePrinter.m Outdated
Comment thread package/ios/ThePrinter.m Outdated
@tr3v3r

tr3v3r commented Jun 27, 2025

Copy link
Copy Markdown
Owner

Hello @tolypash

Thank you very much for the contribution!

I've made a short review and added a couple of comments. Please check once you have time.

@tolypash

Copy link
Copy Markdown
Contributor Author

Happy to contribute
Thanks @tr3v3r for the review, can you please check again now after my changes :)

@tr3v3r
tr3v3r merged commit abddc8e into tr3v3r:main Jun 27, 2025
0 of 5 checks passed
@tr3v3r

tr3v3r commented Jun 27, 2025

Copy link
Copy Markdown
Owner

Just released a new version.
@tolypash

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.

2 participants