Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to obtain the cache path of an image. #351

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class FastImageViewConverter {
put("stretch", ScaleType.FIT_XY);
put("center", ScaleType.CENTER);
}};



// Resolve the source uri to a file path that android understands.
static FastImageSource getImageSource(Context context, ReadableMap source) {
return new FastImageSource(context, source.getString("uri"), getHeaders(source));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

import android.app.Activity;

import android.support.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.views.imagehelper.ImageSource;

import java.io.File;

class FastImageViewModule extends ReactContextBaseJavaModule {

private static final String REACT_CLASS = "FastImageView";
private static final String ERROR_LOAD_FAILED = "ERROR_LOAD_FAILED";

FastImageViewModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -33,6 +43,7 @@ public void preload(final ReadableArray sources) {
public void run() {
for (int i = 0; i < sources.size(); i++) {
final ReadableMap source = sources.getMap(i);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should keep the author's code style, by removing this empty line between variable declarations ;)

final FastImageSource imageSource = FastImageViewConverter.getImageSource(activity, source);

Glide
Expand All @@ -45,12 +56,47 @@ public void run() {
// - data:image/png;base64
.load(
imageSource.isBase64Resource() ? imageSource.getSource() :
imageSource.isResource() ? imageSource.getUri() : imageSource.getGlideUrl()
imageSource.isResource() ? imageSource.getUri() : imageSource.getGlideUrl()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing, keep the indentation here please :3

)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaaand this empty line ;)

.apply(FastImageViewConverter.getOptions(source))
.preload();
}
}
});
}

@ReactMethod
public void loadImage(final ReadableMap source, final Promise promise) {
final Activity activity = getCurrentActivity();
if (activity == null) return;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
final FastImageSource imageSource = FastImageViewConverter.getImageSource(activity, source);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And finally removing this empty line

final GlideUrl glideUrl = imageSource.getGlideUrl();

Glide
.with(activity.getApplicationContext())
.asFile()
.load(glideUrl)
.apply(FastImageViewConverter.getOptions(source))
.listener(new RequestListener<File>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
promise.reject(ERROR_LOAD_FAILED, e);
return false;
}

@Override
public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
promise.resolve(resource.getAbsolutePath());
return false;
}
})
.submit();
}
});
}
}
53 changes: 53 additions & 0 deletions ios/FastImage/FFFastImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,58 @@ - (FFFastImageView*)view {
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:urls];
}

RCT_REMAP_METHOD(
loadImage,
loadImageWithSource: (nonnull FFFastImageSource *)source resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
) {
SDWebImageManager *imageManager = [SDWebImageManager sharedManager];
NSString *cacheKey = [imageManager cacheKeyForURL:source.url];
NSString *imagePath = [imageManager.imageCache defaultCachePathForKey:cacheKey];

// set headers
[source.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString* header, BOOL *stop) {
[imageManager.imageDownloader setValue:header forHTTPHeaderField:key];
}];

// set options
SDWebImageOptions options = 0;
options |= SDWebImageRetryFailed;
switch (source.priority) {
case FFFPriorityLow:
options |= SDWebImageLowPriority;
break;
case FFFPriorityNormal:
// Priority is normal by default.
break;
case FFFPriorityHigh:
options |= SDWebImageHighPriority;
break;
}

switch (source.cacheControl) {
case FFFCacheControlWeb:
options |= SDWebImageRefreshCached;
break;
case FFFCacheControlCacheOnly:
options |= SDWebImageCacheMemoryOnly;
break;
case FFFCacheControlImmutable:
break;
}

// load image
[imageManager loadImageWithURL:source.url options:options progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
if (error != nil) {
reject(@"FastImage", @"Failed to load image", error);
return;
}

// store image manually (since image manager may call the completion block before storing it in the disk cache)
[imageManager.imageCache storeImage:image forKey:cacheKey completion:^{
resolve(imagePath);
}];
}];
}

@end

2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ interface FastImageStatic extends React.ComponentClass<FastImageProperties> {
}

preload(sources: FastImageSource[]): void

loadImage(source: FastImageSource): Promise<string>
}

declare var FastImage: FastImageStatic
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ FastImage.preload = sources => {
FastImageViewNativeModule.preload(sources)
}

FastImage.loadImage = source => {
return FastImageViewNativeModule.loadImage(source)
}

FastImage.defaultProps = {
resizeMode: FastImage.resizeMode.cover,
}
Expand Down