Skip to content

Commit 5257627

Browse files
committed
android 本地图片显示
android 本地图片显示
1 parent 5ead126 commit 5257627

File tree

8 files changed

+202
-1
lines changed

8 files changed

+202
-1
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
android/*.iml
3+
android/build
4+
android/.idea/
5+
.DS_Store
6+
.idea/

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,53 @@
1+
12
# react-native-local-image
2-
react native 显示本地图片
3+
4+
## Getting started
5+
6+
`$ npm install react-native-local-image --save`
7+
8+
### Mostly automatic installation
9+
10+
`$ react-native link react-native-local-image`
11+
12+
### Manual installation
13+
14+
15+
#### iOS
16+
17+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
18+
2. Go to `node_modules``react-native-local-image` and add `RNInputRecord.xcodeproj`
19+
3. In XCode, in the project navigator, select your project. Add `libRNInputRecord.a` to your project's `Build Phases``Link Binary With Libraries`
20+
4. Run your project (`Cmd+R`)<
21+
22+
#### Android
23+
24+
1. Open up `android/app/src/main/java/[...]/MainActivity.java`
25+
- Add `import com.reactlibrary.RNInputRecordPackage;` to the imports at the top of the file
26+
- Add `new RNInputRecordPackage()` to the list returned by the `getPackages()` method
27+
2. Append the following lines to `android/settings.gradle`:
28+
```
29+
include ':react-native-local-image'
30+
project(':react-native-local-image').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-local-image/android')
31+
```
32+
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
33+
```
34+
compile project(':react-native-local-image')
35+
```
36+
37+
#### Windows
38+
[Read it! :D](https://github.com/ReactWindows/react-native)
39+
40+
1. In Visual Studio add the `RNInputRecord.sln` in `node_modules/react-native-local-image/windows/RNInputRecord.sln` folder to their solution, reference from their app.
41+
2. Open up your `MainPage.cs` app
42+
- Add `using Com.Reactlibrary.RNInputRecord;` to the usings at the top of the file
43+
- Add `new RNInputRecordPackage()` to the `List<IReactPackage>` returned by the `Packages` method
44+
45+
46+
## Usage
47+
```javascript
48+
import RNInputRecord from 'react-native-local-image';
49+
50+
// TODO: What to do with the module?
51+
RNInputRecord;
52+
```
53+

android/build.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
buildscript {
3+
repositories {
4+
jcenter()
5+
}
6+
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.3.1'
9+
}
10+
}
11+
12+
apply plugin: 'com.android.library'
13+
14+
android {
15+
compileSdkVersion 23
16+
buildToolsVersion "23.0.1"
17+
18+
defaultConfig {
19+
minSdkVersion 16
20+
targetSdkVersion 22
21+
versionCode 1
22+
versionName "1.0"
23+
}
24+
lintOptions {
25+
abortOnError false
26+
}
27+
}
28+
29+
repositories {
30+
mavenCentral()
31+
}
32+
33+
dependencies {
34+
compile 'com.facebook.react:react-native:+'
35+
}
36+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.local.image">
4+
5+
</manifest>
6+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.local.image;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.BitmapFactory;
5+
import android.util.Log;
6+
import android.widget.ImageView;
7+
8+
import com.facebook.react.uimanager.SimpleViewManager;
9+
import com.facebook.react.uimanager.ThemedReactContext;
10+
import com.facebook.react.uimanager.annotations.ReactProp;
11+
12+
/**
13+
* Created by dowin on 2017/7/7.
14+
*/
15+
16+
public class RCTLocalImageViewManager extends SimpleViewManager<ImageView> {
17+
18+
final static String RCT_CLASS = "RNLocalImage";
19+
20+
@Override
21+
public String getName() {
22+
return RCT_CLASS;
23+
}
24+
25+
@Override
26+
protected ImageView createViewInstance(ThemedReactContext reactContext) {
27+
ImageView imageView = new ImageView(reactContext);
28+
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
29+
return imageView;
30+
}
31+
32+
@ReactProp(name = "path")
33+
public void setPath(ImageView view, String path) {
34+
Log.i(RCT_CLASS, "" + path);
35+
Bitmap bm = BitmapFactory.decodeFile(path);
36+
view.setImageBitmap(bm);
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
package com.local.image;
3+
4+
import com.facebook.react.ReactPackage;
5+
import com.facebook.react.bridge.JavaScriptModule;
6+
import com.facebook.react.bridge.NativeModule;
7+
import com.facebook.react.bridge.ReactApplicationContext;
8+
import com.facebook.react.uimanager.ViewManager;
9+
10+
import java.util.Arrays;
11+
import java.util.Collections;
12+
import java.util.List;
13+
public class RCTLocalPackage implements ReactPackage {
14+
@Override
15+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
16+
return Collections.emptyList();
17+
}
18+
19+
@Override
20+
public List<Class<? extends JavaScriptModule>> createJSModules() {
21+
return Collections.emptyList();
22+
}
23+
24+
@Override
25+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
26+
return Arrays.<ViewManager>asList(new RCTLocalImageViewManager());
27+
}
28+
}

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React, {Component, PropTypes} from 'react';
2+
import {View, requireNativeComponent,NativeModules} from 'react-native';
3+
4+
export default class RNLocalImage extends Component {
5+
6+
render() {
7+
return (
8+
<RNLocalImageApi
9+
{...this.props}
10+
/>);
11+
}
12+
}
13+
RNLocalImage.propTypes = {
14+
...View.propTypes,
15+
path:PropTypes.string,
16+
17+
};
18+
const RNLocalImageApi = requireNativeComponent('RNLocalImage', RNLocalImage);

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
{
3+
"name": "react-native-local-image",
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [
11+
"react-native"
12+
],
13+
"author": "",
14+
"license": "",
15+
"peerDependencies": {
16+
"react-native": "^0.41.2"
17+
}
18+
}

0 commit comments

Comments
 (0)