App.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import React, {Component} from 'react';
  2. import {
  3. StyleSheet,
  4. Text,
  5. View,
  6. Image,
  7. ScrollView,
  8. TouchableOpacity,
  9. Dimensions
  10. } from 'react-native';
  11. import SYImagePicker from 'react-native-syan-image-picker'
  12. const {width} = Dimensions.get('window');
  13. export default class App extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. photos: []
  18. };
  19. }
  20. handleOpenImagePicker = () => {
  21. SYImagePicker.showImagePicker({imageCount: 1,isCrop:true, isRecordSelected: true}, (err, photos) => {
  22. console.log(err, photos);
  23. if (!err) {
  24. this.setState({
  25. photos
  26. })
  27. }
  28. })
  29. };
  30. /**
  31. * 使用方式sync/await
  32. * 相册参数暂时只支持默认参数中罗列的属性;
  33. * @returns {Promise<void>}
  34. */
  35. handleAsyncSelectPhoto = async () => {
  36. SYImagePicker.removeAllPhoto()
  37. try {
  38. const photos = await SYImagePicker.asyncShowImagePicker({imageCount: 1, isCrop: true, showCropCircle: true});
  39. // 选择成功
  40. this.setState({
  41. photos: [...this.state.photos, ...photos]
  42. })
  43. } catch (err) {
  44. // 取消选择,err.message为"取消"
  45. }
  46. };
  47. handlePromiseSelectPhoto = () => {
  48. SYImagePicker.asyncShowImagePicker({imageCount: 3, enableBase64: true})
  49. .then(photos => {
  50. console.log(photos);
  51. const arr = photos.map(v=>{
  52. return { ...v, enableBase64:true}
  53. });
  54. // 选择成功
  55. this.setState({
  56. photos: [...this.state.photos, ...arr]
  57. })
  58. })
  59. .catch(err => {
  60. // 取消选择,err.message为"取消"
  61. })
  62. };
  63. handleLaunchCamera = () => {
  64. SYImagePicker.openCamera({isCrop: true, showCropCircle: true, showCropFrame: false}, (err, photos) => {
  65. console.log(err, photos);
  66. if (!err) {
  67. this.setState({
  68. photos: [...this.state.photos, ...photos]
  69. })
  70. }
  71. })
  72. };
  73. handleDeleteCache = () => {
  74. SYImagePicker.deleteCache()
  75. };
  76. render() {
  77. const {photos} = this.state;
  78. return (
  79. <View style={styles.container}>
  80. <View style={styles.scroll}>
  81. <Button title={'拍照'} onPress={this.handleLaunchCamera}/>
  82. <Button title={'选择照片'} onPress={this.handleOpenImagePicker}/>
  83. <Button title={'选择照片(Async)'} onPress={this.handleAsyncSelectPhoto}/>
  84. <Button title={'选择照片(Promise)带base64'} onPress={this.handlePromiseSelectPhoto}/>
  85. <Button title={'缓存清除'} onPress={this.handleDeleteCache}/>
  86. </View>
  87. <ScrollView style={{flex: 1}} contentContainerStyle={styles.scroll}>
  88. {photos.map((photo, index) => {
  89. let source = { uri: photo.uri };
  90. if (photo.enableBase64) {
  91. source = { uri: photo.base64 };
  92. }
  93. return (
  94. <Image
  95. key={`image-${index}`}
  96. style={styles.image}
  97. source={source}
  98. resizeMode={"contain"}
  99. />
  100. )
  101. })}
  102. </ScrollView>
  103. </View>
  104. );
  105. }
  106. }
  107. const Button = ({title, onPress}) => {
  108. return (
  109. <TouchableOpacity
  110. style={styles.btn}
  111. onPress={onPress}
  112. >
  113. <Text style={{color: '#fff', fontSize: 16}}>{title}</Text>
  114. </TouchableOpacity>
  115. )
  116. };
  117. const styles = StyleSheet.create({
  118. container: {
  119. flex: 1,
  120. backgroundColor: '#F5FCFF',
  121. paddingTop: 40
  122. },
  123. btn: {
  124. backgroundColor: '#FDA549',
  125. justifyContent: 'center',
  126. alignItems: 'center',
  127. height: 44,
  128. paddingHorizontal: 12,
  129. margin: 5,
  130. borderRadius: 22
  131. },
  132. scroll: {
  133. padding: 5,
  134. flexWrap: 'wrap',
  135. flexDirection: 'row'
  136. },
  137. image: {
  138. margin: 10,
  139. width: (width - 80) / 3,
  140. height: (width - 80) / 3,
  141. backgroundColor: '#F0F0F0'
  142. },
  143. });