YH_Image.js
2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React from 'react';
import ReactNative from 'react-native';
const {
Component,
} = React;
const {
Image,
PixelRatio,
Platform,
} = ReactNative;
const YH_ImageView = ReactNative.requireNativeComponent('YH_ImageView', null);
const YH_BackgroundView = (Platform.OS === 'ios') ? null : ReactNative.requireNativeComponent('YH_BackgroundView', null);
/**
* 原生图片
* 圆角设置规则:
* ios写法: masksToBounds={true} style中设置borderRadius圆角值
* android写法: radius={{'topLeft':'20','topRight':'20','bottomRight':'20','bottomLeft':'20'}}
*
*
**/
export default class YH_Image extends Component {
constructor(props) {
super (props);
}
static getSlicedUrl(src, width, height, mode = 1) {
if (!src) {
return '';
}
width = PixelRatio.getPixelSizeForLayoutSize(width);
height = PixelRatio.getPixelSizeForLayoutSize(height);
let newSrc = src;
if (src.indexOf('imageView') === -1 && src.indexOf('imageMogr') === -1) {
newSrc = src + '?imageView2/' + mode + '/w/' + width + '/h/' + height;
} else {
newSrc = src.replace('{mode}', mode)
.replace(/{width}/g, width)
.replace(/{height}/g, height);
}
// __DEV__ && console.log(newSrc);
return newSrc;
}
render() {
if (Platform.OS === 'ios') {
return (
<YH_ImageView
{...this.props}
/>
);
}
else{
let {isBackground} = this.props;
if(isBackground && isBackground == true){
// return null;
return (
<YH_BackgroundView
{...this.props}
/>
);
}
else{
return (
<YH_ImageView
{...this.props}
/>
);
}
}
}
}