Skip to content

Commit 03c50f0

Browse files
authored
feat: Add tint color support.
fix DylanVann#124
1 parent 161ba99 commit 03c50f0

File tree

9 files changed

+93
-0
lines changed

9 files changed

+93
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ A React Native style. Supports using `borderRadius`.
191191
If true will fallback to using `Image`.
192192
In this case the image will still be styled and laid out the same way as `FastImage`.
193193

194+
---
195+
196+
### `tintColor?: number | string`
197+
198+
If supplied, changes the color of all the non-transparent pixels to the given color.
199+
194200
## Static Methods
195201

196202
### `FastImage.preload: (source[]) => void`

android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.content.Context;
5+
import android.graphics.PorterDuff;
56
import android.os.Build;
67

78
import com.bumptech.glide.Glide;
@@ -108,6 +109,15 @@ public void setSrc(FastImageViewWithUrl view, @Nullable ReadableMap source) {
108109
}
109110
}
110111

112+
@ReactProp(name = "tintColor", customType = "Color")
113+
public void setTintColor(FastImageViewWithUrl view, @Nullable Integer color) {
114+
if (color == null) {
115+
view.clearColorFilter();
116+
} else {
117+
view.setColorFilter(color, PorterDuff.Mode.SRC_IN);
118+
}
119+
}
120+
111121
@ReactProp(name = "resizeMode")
112122
public void setResizeMode(FastImageViewWithUrl view, String resizeMode) {
113123
final FastImageViewWithUrl.ScaleType scaleType = FastImageViewConverter.getScaleType(resizeMode);

ios/FastImage/FFFastImageView.h

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
@property (nonatomic, copy) RCTDirectEventBlock onFastImageLoadEnd;
2626
@property (nonatomic, assign) RCTResizeMode resizeMode;
2727
@property (nonatomic, strong) FFFastImageSource *source;
28+
@property (nonatomic, strong) UIColor *imageColor;
2829

2930
@end
3031

ios/FastImage/FFFastImageView.m

+25
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ - (void)setOnFastImageLoadStart:(RCTDirectEventBlock)onFastImageLoadStart {
5959
}
6060
}
6161

62+
- (void)setImageColor:(UIColor *)imageColor {
63+
if (imageColor != nil) {
64+
_imageColor = imageColor;
65+
super.image = [self makeImage:super.image withTint:self.imageColor];
66+
}
67+
}
68+
69+
- (UIImage*)makeImage:(UIImage *)image withTint:(UIColor *)color {
70+
UIImage *newImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
71+
UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);
72+
[color set];
73+
[newImage drawInRect:CGRectMake(0, 0, image.size.width, newImage.size.height)];
74+
newImage = UIGraphicsGetImageFromCurrentImageContext();
75+
UIGraphicsEndImageContext();
76+
return newImage;
77+
}
78+
79+
- (void)setImage:(UIImage *)image {
80+
if (self.imageColor != nil) {
81+
super.image = [self makeImage:image withTint:self.imageColor];
82+
} else {
83+
super.image = image;
84+
}
85+
}
86+
6287
- (void)sendOnLoad:(UIImage *)image {
6388
self.onLoadEvent = @{
6489
@"width":[NSNumber numberWithDouble:image.size.width],

ios/FastImage/FFFastImageViewManager.m

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ - (FFFastImageView*)view {
1818
RCT_EXPORT_VIEW_PROPERTY(onFastImageError, RCTDirectEventBlock)
1919
RCT_EXPORT_VIEW_PROPERTY(onFastImageLoad, RCTDirectEventBlock)
2020
RCT_EXPORT_VIEW_PROPERTY(onFastImageLoadEnd, RCTDirectEventBlock)
21+
RCT_REMAP_VIEW_PROPERTY(tintColor, imageColor, UIColor)
2122

2223
RCT_EXPORT_METHOD(preload:(nonnull NSArray<FFFastImageSource *> *)sources)
2324
{

react-native-fast-image-example/src/FastImageExamples.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import FeatureText from './FeatureText'
99
import ProgressExample from './ProgressExample'
1010
import PreloadExample from './PreloadExample'
1111
import ResizeModeExample from './ResizeModeExample'
12+
import TintColorExample from './TintColorExample'
1213
import LocalImagesExample from './LocalImagesExample'
1314
import StatusBarUnderlay, { STATUS_BAR_HEIGHT } from './StatusBarUnderlay'
1415
import AutoSizeExample from './AutoSizeExample'
@@ -35,6 +36,7 @@ const FastImageExample = () => (
3536
<ProgressExample />
3637
<PreloadExample />
3738
<ResizeModeExample />
39+
<TintColorExample />
3840
<LocalImagesExample />
3941
<AutoSizeExample />
4042
</View>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react'
2+
import { StyleSheet, View} from 'react-native'
3+
import withCacheBust from './withCacheBust'
4+
import FastImage from 'react-native-fast-image'
5+
import Section from './Section'
6+
import SectionFlex from './SectionFlex'
7+
import FeatureText from './FeatureText'
8+
import LogoImage from './images/logo.png'
9+
10+
const TintColorExample = ({ onPressReload }) => (
11+
<View>
12+
<Section>
13+
<FeatureText text="Images with tint color." />
14+
<FeatureText text="All non-transparent pixels are changed to the color." />
15+
</Section>
16+
<SectionFlex onPress={onPressReload}>
17+
<FastImage
18+
style={styles.image}
19+
tintColor={'green'}
20+
source={LogoImage}
21+
/>
22+
<FastImage
23+
style={styles.image}
24+
tintColor={'#9324c3'}
25+
source={LogoImage}
26+
/>
27+
<FastImage
28+
style={styles.image}
29+
tintColor={'rgba(0,0,0,0.5)'}
30+
source={LogoImage}
31+
/>
32+
</SectionFlex>
33+
</View>
34+
)
35+
36+
const styles = StyleSheet.create({
37+
image: {
38+
flex: 1,
39+
height: 100,
40+
margin: 10,
41+
},
42+
})
43+
44+
export default withCacheBust(TintColorExample)
Loading

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const FastImageViewNativeModule = NativeModules.FastImageView
1313

1414
function FastImageBase({
1515
source,
16+
tintColor,
1617
onLoadStart,
1718
onProgress,
1819
onLoad,
@@ -31,6 +32,7 @@ function FastImageBase({
3132
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
3233
<Image
3334
{...props}
35+
tintColor={tintColor}
3436
style={StyleSheet.absoluteFill}
3537
source={resolvedSource}
3638
onLoadStart={onLoadStart}
@@ -48,6 +50,7 @@ function FastImageBase({
4850
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
4951
<FastImageView
5052
{...props}
53+
tintColor={tintColor}
5154
style={StyleSheet.absoluteFill}
5255
source={resolvedSource}
5356
onFastImageLoadStart={onLoadStart}
@@ -118,6 +121,7 @@ const FastImageSourcePropType = PropTypes.shape({
118121
FastImage.propTypes = {
119122
...ViewPropTypes,
120123
source: PropTypes.oneOfType([FastImageSourcePropType, PropTypes.number]),
124+
tintColor: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
121125
onLoadStart: PropTypes.func,
122126
onProgress: PropTypes.func,
123127
onLoad: PropTypes.func,

0 commit comments

Comments
 (0)