Skip to content

Commit a7492a4

Browse files
author
farfromrefug
committed
fix: ImagePipeline.getCacheKey working correctly on iOS / Android
1 parent de7d073 commit a7492a4

File tree

3 files changed

+50
-44
lines changed

3 files changed

+50
-44
lines changed

src/image/index.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ImageInfo as ImageInfoBase,
1313
ImagePipelineConfigSetting,
1414
ScaleType,
15+
SrcType,
1516
aspectRatioProperty,
1617
backgroundUriProperty,
1718
blurDownSamplingProperty,
@@ -453,7 +454,6 @@ export class Img extends ImageBase {
453454
public async updateImageUri() {
454455
const imagePipeLine = getImagePipeline();
455456
const cacheKey = this.cacheKey;
456-
const src = this.src;
457457
if (cacheKey) {
458458
// const isInCache = imagePipeLine.isInBitmapMemoryCache(uri);
459459
// // if (isInCache) {

src/image/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ export class ImagePipeline {
363363
* Returns the actual cache key for url + context
364364
* this is an iOS feature because imageView properties are used for the cache key
365365
*/
366-
getCacheKey(uri: string, context): string;
366+
getCacheKey(uri: string, options: Partial<Img>): string;
367367

368368
/**
369369
* Returns whether the image is stored in the disk cache.

src/image/index.ios.ts

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,52 @@ export function initialize(config?: ImagePipelineConfigSetting): void {
101101
}
102102
export function shutDown(): void {}
103103

104+
function getContextFromOptions(options: Partial<Img>) {
105+
const context: NSDictionary<string, any> = NSMutableDictionary.dictionary();
106+
const transformers = [];
107+
if (options.decodeWidth || options.decodeHeight) {
108+
//@ts-ignore
109+
transformers.push(NSImageDecodeSizeTransformer.transformerWithDecodeWidthDecodeHeight(options.decodeWidth || options.decodeHeight, options.decodeHeight || options.decodeWidth));
110+
}
111+
if (options.tintColor) {
112+
transformers.push(SDImageTintTransformer.transformerWithColor(options.tintColor.ios));
113+
}
114+
if (options.blurRadius) {
115+
transformers.push(SDImageBlurTransformer.transformerWithRadius(options.blurRadius));
116+
}
117+
if (options.roundAsCircle === true) {
118+
//@ts-ignore
119+
transformers.push(NSImageRoundAsCircleTransformer.transformer());
120+
}
121+
if (options.imageRotation !== 0 && !isNaN(options.imageRotation)) {
122+
transformers.push(SDImageRotationTransformer.transformerWithAngleFitSize(-options.imageRotation * (Math.PI / 180), true));
123+
}
124+
if (options.roundBottomLeftRadius || options.roundBottomRightRadius || options.roundTopLeftRadius || options.roundTopRightRadius) {
125+
transformers.push(
126+
//@ts-ignore
127+
NSImageRoundCornerTransformer.transformerWithTopLefRadiusTopRightRadiusBottomRightRadiusBottomLeftRadius(
128+
layout.toDeviceIndependentPixels(options.roundTopLeftRadius),
129+
layout.toDeviceIndependentPixels(options.roundTopRightRadius),
130+
layout.toDeviceIndependentPixels(options.roundBottomRightRadius),
131+
layout.toDeviceIndependentPixels(options.roundBottomLeftRadius)
132+
)
133+
);
134+
}
135+
if (options.mCIFilter) {
136+
transformers.push(SDImageFilterTransformer.transformerWithFilter(options.mCIFilter));
137+
}
138+
if (transformers.length > 0) {
139+
context.setValueForKey(SDImagePipelineTransformer.transformerWithTransformers(transformers), SDWebImageContextImageTransformer);
140+
}
141+
return context;
142+
}
143+
104144
export class ImagePipeline {
105145
private mIos: SDImageCache = SDImageCache.sharedImageCache;
106146
constructor() {}
107147

108-
getCacheKey(uri: string, context) {
148+
getCacheKey(uri: string, options: Partial<Img>) {
149+
const context = getContextFromOptions(options);
109150
return SDWebImageManager.sharedManager.cacheKeyForURLContext(NSURL.URLWithString(uri), context);
110151
}
111152

@@ -234,7 +275,7 @@ export class Img extends ImageBase {
234275
return this.mCacheKey;
235276
}
236277
protected mImageSourceAffectsLayout: boolean = true;
237-
protected mCIFilter: CIFilter;
278+
mCIFilter: CIFilter;
238279
public createNativeView() {
239280
const result = this.animatedImageView ? SDAnimatedImageView.new() : UIImageView.new();
240281
result.contentMode = UIViewContentMode.ScaleAspectFit;
@@ -446,7 +487,7 @@ export class Img extends ImageBase {
446487
this.placeholderImage = this.getUIImage(this.placeholderImageUri);
447488
this._setNativeImage(this.placeholderImage, animate);
448489
}
449-
490+
450491
if (this.noCache) {
451492
// const key = uri.absoluteString;
452493
// const imagePipeLine = getImagePipeline();
@@ -459,48 +500,13 @@ export class Img extends ImageBase {
459500
if (this.alwaysFade === true) {
460501
options |= SDWebImageOptions.ForceTransition;
461502
}
462-
const context: NSDictionary<string, any> = NSMutableDictionary.dictionary();
463-
const transformers = [];
464503
if (this.progressiveRenderingEnabled === true) {
465504
options = options | SDWebImageOptions.ProgressiveLoad;
466505
}
467-
if (this.decodeWidth || this.decodeHeight) {
468-
//@ts-ignore
469-
transformers.push(NSImageDecodeSizeTransformer.transformerWithDecodeWidthDecodeHeight(this.decodeWidth || this.decodeHeight, this.decodeHeight || this.decodeWidth));
470-
}
471-
if (this.tintColor) {
472-
transformers.push(SDImageTintTransformer.transformerWithColor(this.tintColor.ios));
473-
}
474-
if (this.blurRadius) {
475-
transformers.push(SDImageBlurTransformer.transformerWithRadius(this.blurRadius));
476-
}
477-
if (this.roundAsCircle === true) {
478-
//@ts-ignore
479-
transformers.push(NSImageRoundAsCircleTransformer.transformer());
480-
}
481-
if (this.imageRotation !== 0 && !isNaN(this.imageRotation)) {
482-
transformers.push(SDImageRotationTransformer.transformerWithAngleFitSize(-this.imageRotation * (Math.PI / 180), true));
483-
}
484-
if (this.roundBottomLeftRadius || this.roundBottomRightRadius || this.roundTopLeftRadius || this.roundTopRightRadius) {
485-
transformers.push(
486-
//@ts-ignore
487-
NSImageRoundCornerTransformer.transformerWithTopLefRadiusTopRightRadiusBottomRightRadiusBottomLeftRadius(
488-
layout.toDeviceIndependentPixels(this.roundTopLeftRadius),
489-
layout.toDeviceIndependentPixels(this.roundTopRightRadius),
490-
layout.toDeviceIndependentPixels(this.roundBottomRightRadius),
491-
layout.toDeviceIndependentPixels(this.roundBottomLeftRadius)
492-
)
493-
);
494-
}
495-
if (this.mCIFilter) {
496-
transformers.push(SDImageFilterTransformer.transformerWithFilter(this.mCIFilter));
497-
}
498-
if (transformers.length > 0) {
499-
if (this.animatedImageView) {
500-
// as we use SDAnimatedImageView all images are loaded as SDAnimatedImage;
501-
options |= SDWebImageOptions.TransformAnimatedImage;
502-
}
503-
context.setValueForKey(SDImagePipelineTransformer.transformerWithTransformers(transformers), SDWebImageContextImageTransformer);
506+
const context = getContextFromOptions(this);
507+
if (this.animatedImageView) {
508+
// as we use SDAnimatedImageView all images are loaded as SDAnimatedImage;
509+
options |= SDWebImageOptions.TransformAnimatedImage;
504510
}
505511
this.mCacheKey = SDWebImageManager.sharedManager.cacheKeyForURLContext(uri, context);
506512
this.nativeImageViewProtected.sd_setImageWithURLPlaceholderImageOptionsContextProgressCompleted(

0 commit comments

Comments
 (0)