SlicedImage.js
1.97 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
'use strict';
import React from 'react';
import ReactNative from 'react-native';
const {
Image,
PixelRatio,
StyleSheet,
} = ReactNative;
export default class SlicedImage extends React.Component {
static propTypes = {
source: Image.propTypes.source,
};
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) {
newSrc = src + '?imageView2/' + mode + '/w/' + width + '/h/' + height;
} else {
newSrc = src.replace('{mode}', mode)
.replace('{width}', width)
.replace('{height}', height);
}
__DEV__ && console.log(newSrc);
return newSrc;
}
constructor(props) {
super (props);
// http://developer.qiniu.com/code/v6/api/kodo-api/index.html#image
this.mode = 1;
}
setNativeProps(nativeProps) {
this._root.setNativeProps(nativeProps);
}
_generateImageUrl(src) {
let style = StyleSheet.flatten(this.props.style);
let {width, height} = style;
let newSrc = SlicedImage.getSlicedUrl(src, width, height, this.mode);
return newSrc;
}
_isRemoteImageSrc(src) {
let prefix = 'http';
if (src.toLowerCase().substr(0, prefix.length) === prefix) {
return true;
}
return false;
}
render() {
let {source} = this.props;
if (source && source.uri && this._isRemoteImageSrc(source.uri)) {
let uri = this._generateImageUrl(source.uri);
return (
<Image ref={component => this._root = component} {...this.props} source={{uri}}/>
);
}
return (
<Image ref={component => this._root = component} {...this.props}/>
);
}
}