mirror of
https://github.com/status-im/react-native-fast-image.git
synced 2025-02-22 19:28:32 +00:00
Enables prefetching images
This commit is contained in:
parent
1d25cb23a5
commit
4c87fc0337
11
FastImage.js
11
FastImage.js
@ -1,5 +1,5 @@
|
||||
import React, { PropTypes, Component } from 'react'
|
||||
import { requireNativeComponent, Image, View } from 'react-native'
|
||||
import { requireNativeComponent, Image, NativeModules, View } from 'react-native'
|
||||
|
||||
const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource')
|
||||
|
||||
@ -50,6 +50,15 @@ FastImage.priority = {
|
||||
high: 'high',
|
||||
}
|
||||
|
||||
FastImage.prefetch = function(source) {
|
||||
let urls = source;
|
||||
if (typeof source === 'string') {
|
||||
urls = [source];
|
||||
}
|
||||
|
||||
NativeModules.FastImageView.prefetch(urls);
|
||||
}
|
||||
|
||||
const FastImageSourcePropType = PropTypes.shape({
|
||||
uri: PropTypes.string,
|
||||
headers: PropTypes.objectOf(PropTypes.string),
|
||||
|
@ -0,0 +1,39 @@
|
||||
package com.dylanvann.fastimage;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
|
||||
public class FastImageViewModule extends ReactContextBaseJavaModule {
|
||||
public FastImageViewModule(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "FastImageView";
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void prefetch(final ReadableArray urls) {
|
||||
final Activity activity = getCurrentActivity();
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < urls.size(); i++) {
|
||||
Glide
|
||||
.with(activity.getApplicationContext())
|
||||
.load(urls.getString(i))
|
||||
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
|
||||
.preload();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package com.dylanvann.fastimage;
|
||||
|
||||
import com.dylanvann.fastimage.FastImageViewModule;
|
||||
|
||||
import com.facebook.react.ReactPackage;
|
||||
import com.facebook.react.bridge.NativeModule;
|
||||
import com.facebook.react.bridge.JavaScriptModule;
|
||||
@ -10,10 +12,9 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class FastImageViewPackage implements ReactPackage {
|
||||
|
||||
@Override
|
||||
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
|
||||
return Collections.emptyList();
|
||||
return Collections.<NativeModule>singletonList(new FastImageViewModule(reactContext));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,6 +1,8 @@
|
||||
#import "FFFastImageViewManager.h"
|
||||
#import "FFFastImageView.h"
|
||||
|
||||
#import <SDWebImage/SDWebImagePrefetcher.h>
|
||||
|
||||
@implementation FFFastImageViewManager
|
||||
|
||||
RCT_EXPORT_MODULE(FastImageView)
|
||||
@ -16,5 +18,9 @@ RCT_EXPORT_VIEW_PROPERTY(source, FFFastImageSource);
|
||||
RCT_EXPORT_VIEW_PROPERTY(resizeMode, RCTResizeMode);
|
||||
RCT_EXPORT_VIEW_PROPERTY(onFastImageError, RCTDirectEventBlock);
|
||||
RCT_EXPORT_VIEW_PROPERTY(onFastImageLoad, RCTDirectEventBlock);
|
||||
RCT_EXPORT_METHOD(prefetch:(nonnull NSArray<NSURL *> *)urls)
|
||||
{
|
||||
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:urls];
|
||||
}
|
||||
|
||||
@end
|
||||
|
Loading…
x
Reference in New Issue
Block a user