-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Image] Add a way to prefetch remote images to cache with Image.prefetch
Adds `Image.prefetch` to prefetch remote images before they are used in an actual `Image` component. Test Plan: - Image demo in UIExplorer on Android and iOS. Performance is better on iOS but in both cases you can see that loading a prefetched image is faster than a non-prefetched one. - Using this in production - CI
- Loading branch information
Showing
7 changed files
with
169 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
ReactAndroid/src/main/java/com/facebook/react/modules/image/BUCK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
include_defs('//ReactAndroid/DEFS') | ||
|
||
android_library( | ||
name = 'image', | ||
srcs = glob(['*.java']), | ||
deps = [ | ||
react_native_target('java/com/facebook/react/bridge:bridge'), | ||
react_native_target('java/com/facebook/react/common:common'), | ||
react_native_dep('libraries/fresco/fresco-react-native:fbcore'), | ||
react_native_dep('libraries/fresco/fresco-react-native:fresco-react-native'), | ||
react_native_dep('libraries/fresco/fresco-react-native:fresco-drawee'), | ||
react_native_dep('libraries/fresco/fresco-react-native:imagepipeline'), | ||
], | ||
visibility = [ | ||
'PUBLIC', | ||
], | ||
) | ||
|
||
project_config( | ||
src_target = ':image', | ||
) |
76 changes: 76 additions & 0 deletions
76
ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.modules.image; | ||
|
||
import android.net.Uri; | ||
|
||
import com.facebook.common.executors.CallerThreadExecutor; | ||
import com.facebook.datasource.BaseDataSubscriber; | ||
import com.facebook.datasource.DataSource; | ||
import com.facebook.datasource.DataSubscriber; | ||
import com.facebook.drawee.backends.pipeline.Fresco; | ||
import com.facebook.imagepipeline.request.ImageRequest; | ||
import com.facebook.imagepipeline.request.ImageRequestBuilder; | ||
import com.facebook.react.bridge.Promise; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
|
||
public class ImageLoaderModule extends ReactContextBaseJavaModule { | ||
|
||
private static final String ERROR_INVALID_URI = "E_INVALID_URI"; | ||
private static final String ERROR_PREFETCH_FAILURE = "E_PREFETCH_FAILURE"; | ||
|
||
public ImageLoaderModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "ImageLoader"; | ||
} | ||
|
||
/** | ||
* Prefetches the given image to the Fresco image disk cache. | ||
* | ||
* @param uriString the URI of the remote image to prefetch | ||
* @param promise the promise that is fulfilled when the image is successfully prefetched | ||
* or rejected when there is an error | ||
*/ | ||
@ReactMethod | ||
public void prefetchImage(String uriString, final Promise promise) { | ||
if (uriString == null || uriString.isEmpty()) { | ||
promise.reject(ERROR_INVALID_URI, "Cannot prefetch an image for an empty URI"); | ||
return; | ||
} | ||
|
||
Uri uri = Uri.parse(uriString); | ||
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).build(); | ||
|
||
DataSource<Void> prefetchSource = Fresco.getImagePipeline().prefetchToDiskCache(request, this); | ||
DataSubscriber<Void> prefetchSubscriber = new BaseDataSubscriber<Void>() { | ||
@Override | ||
protected void onNewResultImpl(DataSource<Void> dataSource) { | ||
if (!dataSource.isFinished()) { | ||
return; | ||
} | ||
promise.resolve(true); | ||
dataSource.close(); | ||
} | ||
|
||
@Override | ||
protected void onFailureImpl(DataSource<Void> dataSource) { | ||
promise.reject(ERROR_PREFETCH_FAILURE, dataSource.getFailureCause()); | ||
dataSource.close(); | ||
} | ||
}; | ||
prefetchSource.subscribe(prefetchSubscriber, CallerThreadExecutor.getInstance()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters