Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mmazzarolo
Created December 1, 2018 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmazzarolo/45b7fc49b9e5dd5007f2090bc9e8cc83 to your computer and use it in GitHub Desktop.
Save mmazzarolo/45b7fc49b9e5dd5007f2090bc9e8cc83 to your computer and use it in GitHub Desktop.
Image decoding limit workaround for RN (Android)
export default class CroppedImage extends Component {
state = {
croppedImageSources: []
};
async componentDidMount() {
const crops = 4;
const croppedImageHeight = imHeight / crops;
const croppedImageSources = [];
for (let i = 0; i <= crops; i++) {
const cropData = {
offset: { x: 0, y: croppedImageHeight * i },
size: { width: imgWidth, height: croppedImageHeight }
};
const croppedImageSource = await asyncCropImage(
this.props.url,
cropData
);
croppedImageSources.push(croppedImageSource);
}
this.setState({ croppedImageSources });
}
render() {
return this.state.croppedImageSources.map(source => (
<Image style={styles.image} source={source} />
));
}
}
const styles = StyleSheet.create({
image: {
width: width,
height: croppedImageHeight
}
});
const asyncCropImage = async (source: string, cropData: any) =>
new Promise((resolve, reject) => {
const onSuccess = imageUri => resolve(imageUri);
const onFailure = err => {
return reject(new Error(err));
};
ImageEditor.cropImage(source, cropData, onSuccess, onFailure);
});
@mmazzarolo
Copy link
Author

The solution above is a simple workaround for fixing the Android image decoding issue.
I collected a few interesting resources about it, but the TLDR is: Android is not able to load correctly images with a really high resolution (e.g.: 800 * 20000) and shows them as blurry/pixelated.

Useful resources:

@mmazzarolo
Copy link
Author

From my testing both Image (Fresco) and FastImage (Glide) have this issue.
If the image is a JPEG you might be able to fix it using resizeMethod="resize".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment