-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Fix - heic context lost #85231
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
Merged
mountiny
merged 4 commits into
Expensify:main
from
callstack-internal:fix/heic-context-lost
Mar 17, 2026
+110
−33
Merged
Fix - heic context lost #85231
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
99faf34
fix silent promise hang and raw error on heic conversion failure
rinej 55abec5
cover heicConverter and useFilesValidation for 48MP heic failure
rinej 2757212
migrate to ImageManipulator to fix depracation error
rinej f635b6e
adjust heic conversion after pr comments
rinej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,85 @@ | ||
| import {manipulateAsync} from 'expo-image-manipulator'; | ||
| import {ImageManipulator} from 'expo-image-manipulator'; | ||
| import {Platform} from 'react-native'; | ||
| import RNFetchBlob from 'react-native-blob-util'; | ||
| import ImageSize from 'react-native-image-size'; | ||
| import Log from '@libs/Log'; | ||
| import getSaveFormat from './getSaveFormat'; | ||
| import type {CropOrRotateImage} from './types'; | ||
|
|
||
| /** | ||
| * Crops and rotates the image on ios/android | ||
| * Crops and rotates the image on ios/android. | ||
| * On iOS, falls back to the original unprocessed image if manipulation fails | ||
| * (e.g. CGContext allocation failure on 48MP photos). | ||
| */ | ||
| const cropOrRotateImage: CropOrRotateImage = (uri, actions, options) => | ||
| new Promise((resolve) => { | ||
| new Promise((resolve, reject) => { | ||
| const format = getSaveFormat(options.type); | ||
| // We need to remove the base64 value from the result, as it is causing crashes on Release builds. | ||
| // More info: https://github.com/Expensify/App/issues/37963#issuecomment-1989260033 | ||
| manipulateAsync(uri, actions, {compress: options.compress, format}).then(({base64, ...result}) => { | ||
| RNFetchBlob.fs.stat(result.uri.replace('file://', '')).then(({size}) => { | ||
| resolve({ | ||
| ...result, | ||
| size, | ||
| type: options.type || 'image/jpeg', | ||
| name: options.name || 'fileName.jpg', | ||
| }); | ||
| const context = ImageManipulator.manipulate(uri); | ||
| for (const action of actions) { | ||
| if ('crop' in action) { | ||
| context.crop(action.crop); | ||
| } else if ('rotate' in action) { | ||
| context.rotate(action.rotate); | ||
| } | ||
| } | ||
| context | ||
| .renderAsync() | ||
| .then((imageRef) => imageRef.saveAsync({compress: options.compress, format})) | ||
| // We need to remove the base64 value from the result, as it is causing crashes on Release builds. | ||
| // More info: https://github.com/Expensify/App/issues/37963#issuecomment-1989260033 | ||
| .then(({base64, ...result}) => { | ||
| RNFetchBlob.fs | ||
| .stat(result.uri.replace('file://', '')) | ||
| .then(({size}) => { | ||
| resolve({ | ||
| ...result, | ||
| size, | ||
| type: options.type || 'image/jpeg', | ||
| name: options.name || 'fileName.jpg', | ||
| }); | ||
| }) | ||
| .catch(reject); | ||
| }) | ||
| .catch((error) => { | ||
| if (Platform.OS !== 'ios') { | ||
| reject(error); | ||
| return; | ||
| } | ||
|
Comment on lines
+44
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you split this up to ios and android files to remove this check?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should I create new PR with that quick fix? |
||
|
|
||
| Log.warn('Error cropping/rotating image, falling back to original', {error: error instanceof Error ? error.message : String(error)}); | ||
| const filePath = uri.replace('file://', ''); | ||
| ImageSize.getSize(uri) | ||
| .then(({width, height}) => { | ||
| RNFetchBlob.fs | ||
| .stat(filePath) | ||
| .then(({size}) => { | ||
| resolve({ | ||
| uri, | ||
| width: width ?? 0, | ||
| height: height ?? 0, | ||
| size, | ||
| type: options.type || 'image/jpeg', | ||
| name: options.name || 'fileName.jpg', | ||
| }); | ||
| }) | ||
| .catch(reject); | ||
| }) | ||
| .catch(() => { | ||
| RNFetchBlob.fs | ||
| .stat(filePath) | ||
| .then(({size}) => { | ||
| resolve({ | ||
| uri, | ||
| width: 0, | ||
| height: 0, | ||
| size, | ||
| type: options.type || 'image/jpeg', | ||
| name: options.name || 'fileName.jpg', | ||
| }); | ||
| }) | ||
| .catch(reject); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| export default cropOrRotateImage; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,32 @@ | ||
| import {manipulateAsync} from 'expo-image-manipulator'; | ||
| import {ImageManipulator} from 'expo-image-manipulator'; | ||
| import getSaveFormat from './getSaveFormat'; | ||
| import type {CropOrRotateImage} from './types'; | ||
|
|
||
| const cropOrRotateImage: CropOrRotateImage = (uri, actions, options) => | ||
| new Promise((resolve) => { | ||
| new Promise((resolve, reject) => { | ||
| const format = getSaveFormat(options.type); | ||
| manipulateAsync(uri, actions, {compress: options.compress, format}).then((result) => { | ||
| fetch(result.uri) | ||
| .then((res) => res.blob()) | ||
| .then((blob) => { | ||
| const file = new File([blob], options.name || 'fileName.jpeg', {type: options.type || 'image/jpeg'}); | ||
| file.uri = URL.createObjectURL(file); | ||
| resolve(file); | ||
| }); | ||
| }); | ||
| const context = ImageManipulator.manipulate(uri); | ||
| for (const action of actions) { | ||
| if ('crop' in action) { | ||
| context.crop(action.crop); | ||
| } else if ('rotate' in action) { | ||
| context.rotate(action.rotate); | ||
| } | ||
| } | ||
| context | ||
| .renderAsync() | ||
| .then((imageRef) => imageRef.saveAsync({compress: options.compress, format})) | ||
| .then((result) => | ||
| fetch(result.uri) | ||
| .then((res) => res.blob()) | ||
| .then((blob) => { | ||
| const file = new File([blob], options.name || 'fileName.jpeg', {type: options.type || 'image/jpeg'}); | ||
| file.uri = URL.createObjectURL(file); | ||
| resolve(file); | ||
| }) | ||
| .catch(reject), | ||
| ) | ||
| .catch(reject); | ||
| }); | ||
|
|
||
| export default cropOrRotateImage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 Cross-Platform Compatibility & Android Side-Effects - Unintended Fallback on Android
Issue: As noted in the issue affected devices and PR description - this logic fix targets an issue existent on iOS mWeb / Native. However, we modified
.native.tsfiles, which executes on both Android and iOS.Why it matters: If an Android device fails to manipulate an image (e.g., encountering an OutOfMemoryError typical of lower-end Android devices loading large bit-maps), this
.catch()block will now swallow the error and pass the massive original image forward.Uploading heavily uncompressed local images on Android will cause immediate "Network Request Failed" errors due to payload size limits, or further OOM crashes in the network layer.
Suggested Fix: Isolate this fallback logic platform-specifically, using platform specific files as per our guidelines. Pseudocode: