标签:
const React = require('react-native'); const { CameraRoll, } = React;
{ first: ..., // (必须的) 在照片中,按反相排序后,在获取的照片的数量 after: ..., // 上一次调用getPhotos返回的指针。cursor groupTypes: ..., // 指定分组类型,过滤结果 // One of ['Album', 'All', 'Event', 'Faces', 'Library', 'PhotoStream', 'SavedPhotos'(default)] groupName: ..., // Specifies filter on group names, like 'Recent Photos' or custom album titles assetType: ... // Specifies filter on assetType // One of ['All', 'Videos', 'Photos'(default)] }
const fetchParams = { first: 25, }
{ edges: [ node: { type: ..., group_name: ..., image: { uri: ..., height: ..., width: ..., isStored: ..., }, timestamp: ..., location { ... }, }, node: { ... }, node: { ... }, ... ], page_info: { has_next_page: ..., start_cursor: ..., end_cursor: ..., } }
storeImages(data) { const assets = data.edges; const images = assets.map( asset => asset.node.image ); this.setState({ images: images, }); },
getInitialState() { return { images: [], }; },
logImageError(err) { console.log(err); },
componentDidMount() { const fetchParams = { first: 25, }; CameraRoll.getPhotos(fetchParams, this.storeImages, this.logImageError); },
const { CameraRoll, StyleSheet, Image, } = React;
render() { return ( <ScrollView style={styles.container}> <View style={styles.imageGrid}> { this.state.images.map(image => <Image style={styles.image} source={{ uri: image.uri }} />) } </View> </ScrollView> ); }
const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F5FCFF', }, imageGrid: { flex: 1, flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'center' }, image: { width: 100, height: 100, margin: 10, }, });
const React = require('react-native'); const { StyleSheet, Text, View, ScrollView, Image, CameraRoll, } = React; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F5FCFF', }, imageGrid: { flex: 1, flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'center' }, image: { width: 100, height: 100, margin: 10, }, }); const reactImageProject = React.createClass({ getInitialState() { return { images: [], }; }, componentDidMount() { const fetchParams = { first: 25, }; CameraRoll.getPhotos(fetchParams, this.storeImages, this.logImageError); }, storeImages(data) { const assets = data.edges; const images = assets.map((asset) => asset.node.image); this.setState({ images: images, }); }, logImageError(err) { console.log(err); }, render() { return ( <ScrollView style={styles.container}> <View style={styles.imageGrid}> { this.state.images.map((image) => <Image style={styles.image} source={{ uri: image.uri }} />) } </View> </ScrollView> ); } });
selectImage(uri) { this.setState({ selected: uri, }); console.log('Selected image: ', uri); },
getInitialState() { return { images: [], selected: '', }; },
const { StyleSheet, Text, View, ScrollView, Image, CameraRoll, TouchableHighlight, } = React; render() { return ( <ScrollView style={styles.container}> <View style={styles.imageGrid}> { this.state.images.map((image) => { return ( <TouchableHighlight onPress={this.selectImage.bind(null, image.uri)}> <Image style={styles.image} source={{ uri: image.uri }} /> </TouchableHighlight> ); }) } </View> </ScrollView> ); }
selectImage(uri) { NativeModules.ReadImageData.readImage(uri, (image) => { this.setState({ selected: image, }); console.log(image); }); },
const React = require('react-native'); const { AppRegistry, StyleSheet, Text, View, ScrollView, Image, CameraRoll, TouchableHighlight, NativeModules, } = React; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F5FCFF', }, imageGrid: { flex: 1, flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'center' }, image: { width: 100, height: 100, margin: 10, } }); const reactImageProject = React.createClass({ getInitialState() { return { images: [], selected: '', }; }, componentDidMount() { const fetchParams = { first: 25, }; CameraRoll.getPhotos(fetchParams, this.storeImages, this.logImageError); }, storeImages(data) { const assets = data.edges; const images = assets.map((asset) => asset.node.image); this.setState({ images: images, }); }, logImageError(err) { console.log(err); }, selectImage(uri) { NativeModules.ReadImageData.readImage(uri, (image) => { this.setState({ selected: image, }); console.log(image); }); }, render() { return ( <ScrollView style={styles.container}> <View style={styles.imageGrid}> { this.state.images.map((image) => { return ( <TouchableHighlight onPress={this.selectImage.bind(null, image.uri)}> <Image style={styles.image} source={{ uri: image.uri }} /> </TouchableHighlight> ); }) } </View> </ScrollView> ); } }); AppRegistry.registerComponent('reactImageProject', () => reactImageProject);
base64: ReactPropTypes.string,
#import "RCTBridgeModule.h" #import <AssetsLibrary/AssetsLibrary.h> #import <UIKit/UIKit.h> @interface ReadImageData : NSObject <RCTBridgeModule> @end @implementation ReadImageData RCT_EXPORT_MODULE(); RCT_EXPORT_METHOD(readImage:(NSString *)input callback:(RCTResponseSenderBlock)callback) { // Create NSURL from uri NSURL *url = [[NSURL alloc] initWithString:input]; // Create an ALAssetsLibrary instance. This provides access to the // videos and photos that are under the control of the Photos application. ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; // Using the ALAssetsLibrary instance and our NSURL object open the image. [library assetForURL:url resultBlock:^(ALAsset *asset) { // Create an ALAssetRepresentation object using our asset // and turn it into a bitmap using the CGImageRef opaque type. CGImageRef imageRef = [asset thumbnail]; // Create UIImageJPEGRepresentation from CGImageRef NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1); // Convert to base64 encoded string NSString *base64Encoded = [imageData base64EncodedStringWithOptions:0]; callback(@[base64Encoded]); } failureBlock:^(NSError *error) { NSLog(@"that didn't work %@", error); }]; } @end
var {View, Text, Image, NativeModules} = React;
NativeModules.ReadImageData.readImage(ourImage.node.image.uri, (image) => { console.log(image) })
fetch('http://your.server/app.php', { method:'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', body: JSON.stringify({imageData:image}) } })
Camera Roll API + Upload From Camera Roll Module
标签:
原文地址:http://blog.csdn.net/cexo425/article/details/51324049