Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Improve Error Handling and Image Conversion in removeBackground Method #9

Merged
merged 1 commit into from
Sep 27, 2024

Conversation

enisit
Copy link
Contributor

@enisit enisit commented Sep 27, 2024

This PR enhances the removeBackground function within the iOS implementation of the ReactNativeBackgroundRemover package. The main goal is to improve error handling and stability during background removal by avoiding forced unwrapping and improving image conversion.

Key Changes:

  1. Error Handling Improvements:

    • Added proper checks and guard statements to prevent crashes due to nil values. This ensures the app won’t crash when an invalid image URL or a failure in loading the image occurs.
    • Clear error messages are returned using the promise rejection system in case of failure during various stages (URL validation, image loading, segmentation results, etc.).
  2. Improved Image Processing:

    • Used CIContext to convert CIImage to CGImage before generating a UIImage, ensuring better compatibility and avoiding crashes on certain devices.
    • Added a scaling check for the mask image to avoid up-scaling when the mask dimensions are smaller than the original image dimensions.
  3. File Handling Enhancements:

    • Changed the file-saving logic to ensure the output image is saved as a PNG to preserve transparency.
    • Improved the temporary file path generation by appending the correct .png file extension.

Detailed Changes:

  • Replaced forced unwrapping of URL and CIImage with guard statements to handle potential failure gracefully.
  • Added a check to prevent up-scaling the mask when the mask image dimensions are smaller than the original image.
  • Improved the conversion of the final CIImage to UIImage by using a CGImage intermediary, ensuring compatibility on all devices.
  • Added specific error codes and messages for each failure point in the background removal process.

Benefits:

  • This patch increases the robustness of the app by preventing potential crashes due to improper image handling and segmentation results.
  • Users with devices experiencing crashes during the image conversion process should experience improved stability.
  • Better file-saving mechanism ensures the final output preserves transparency, leading to higher quality results for users.

Affected Files:

  • packages/react-native-background-remover/ios/ReactNativeBackgroundRemover.swift

-Added check to avoid producing images with large size
-Use CIContext for proper image conversion process to avoid crashes on some devices
Copy link
Owner

@atlj atlj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thank you so much for sending this!

@atlj atlj merged commit 9429ee4 into atlj:main Sep 27, 2024
4 checks passed
@carlosmellado
Copy link

Had to go back to 0.2.0 because on this version removing background was very very unstable and on 0.2.0 was working a lot better. 😭

@atlj
Copy link
Owner

atlj commented Oct 3, 2024

Had to go back to 0.2.0 because on this version removing background was very very unstable and on 0.2.0 was working a lot better. 😭

@carlosmellado that's pretty interesting, could you share some more context? What was unstable? Did you observe any unexpected crashes or performance issues?

@carlosmellado
Copy link

Not crashing but the results of removing background on iOS were very different than the version before. On this new version was not working as expected, like removing parts that not are the background or removing parts of the person in front.
Going back to the previous version resolved those issues and removing background works perfect

@enisit
Copy link
Contributor Author

enisit commented Oct 4, 2024

@carlosmellado our QA reported the same issue. Here is new PR that should solve it. @atlj please check it out.

@atlj
Copy link
Owner

atlj commented Oct 4, 2024

@enisit @carlosmellado FYI, I just published this fix with 0.3.1

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.

3 participants