I have been dealing with the a gradient rectangle over an Image that has a black and a transparent sides, I have been looking about a gradient object in react native and I didn't found, but there is a react-native module that does this, but the problem is that it does work in android the transparency, but in iOS, it doesn't work, it shows white in place of the transparent side
and than I was looking about a native iOS solution, I did but it's a bit complex, and I can't implement in react native this the snippet
CAGradientLayer *gradientMask = [CAGradientLayer layer];
gradientMask.frame = self.imageView.bounds;
gradientMask.colors = @[(id)[UIColor whiteColor].CGColor,
(id)[UIColor clearColor].CGColor];
self.imageView.layer.mask = gradientMask; <-- // looking for a way to achieve this in react native
this is my react native code
<Image ref={r => this.image = r} style={styles.container} source={require('../assets/default_profile_picture.jpg')}>
<LinearGradient ref={r => this.gradiant = r} locations={[0, 1.0]} colors={['rgba(0,0,0,0.00)', 'rgba(0,0,0,0.80)']} style={styles.linearGradient}>
</LinearGradient>
</Image>
I don't know how to pass LinearGradient
to Image
as a mask
In order to change the opacity, you should use the style property opacity of the image component. You can change the transparency from 0.0 to 1.0 whereas 1.0 is the maximum i.e. the image becomes opaque. For example, if you want to bring down the transparency to 50 percent you have to use the value 0.5 for opacity.
Just set opacity: 0.5
for styles.linearGradient
You can use my react-native-image-filter-kit library to achieve this:
import { Image } from 'react-native'
import {
SrcOverComposition,
LinearGradient
} from 'react-native-image-filter-kit'
const masked = (
<SrcOverComposition
resizeCanvasTo={'dstImage'}
dstImage={
<Image
style={{ width: 320, height: 320 }}
source={{ uri: 'https://una.im/CSSgram/img/cacti.jpg' }}
/>
}
srcResizeMode={{ width: 1, height: 0.5 }}
srcAnchor={{ y: 0 }}
srcImage={
<LinearGradient
start={{ x: 0, y: 0 }}
end={{ x: 0, y: '100h' }}
colors={['rgba(0,0,0,0.80)', 'rgba(0,0,0,0.00)']}
/>
}
/>
)
Android:
iOS:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With