Skip to content

Commit 66cbf3c

Browse files
committed
feat: cacheKey support
1 parent c8d5da9 commit 66cbf3c

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

src/image/index.android.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,22 @@ export class Img extends ImageBase {
416416
this.controllerListener = null;
417417
// this.nativeImageViewProtected.setImageURI(null, null);
418418
}
419-
419+
get cacheKey() {
420+
const src = this.src;
421+
if (src && !(src instanceof ImageSource)) {
422+
return getUri(src)
423+
}
424+
return undefined;
425+
}
420426
public updateImageUri() {
421427
const imagePipeLine = getImagePipeline();
428+
const cacheKey = this.cacheKey;
422429
const src = this.src;
423-
if (!(src instanceof ImageSource)) {
424-
const uri = getUri(src);
425-
const isInCache = imagePipeLine.isInBitmapMemoryCache(uri);
426-
if (isInCache) {
427-
imagePipeLine.evictFromCache(uri);
428-
}
430+
if (cacheKey) {
431+
// const isInCache = imagePipeLine.isInBitmapMemoryCache(uri);
432+
// // if (isInCache) {
433+
imagePipeLine.evictFromCache(cacheKey);
434+
// }
429435
}
430436
this.src = null;
431437
this.src = src;

src/image/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ export class Img extends View {
208208
* Android: custom imageRotation
209209
*/
210210
imageRotation: number;
211+
212+
cacheKey: string;
211213
}
212214

213215
/**

src/image/index.ios.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,24 @@ export class ImagePipeline {
104104
return SDWebImageManager.sharedManager.cacheKeyForURLContext(NSURL.URLWithString(uri), context);
105105
}
106106

107-
isInDiskCache(uri: string): boolean {
108-
return this.mIos.diskImageDataExistsWithKey(getUri(uri).absoluteString);
107+
isInDiskCache(key: string): boolean {
108+
return this.mIos.diskImageDataExistsWithKey(key);
109109
}
110110

111-
isInBitmapMemoryCache(uri: string): boolean {
112-
return this.mIos.imageFromMemoryCacheForKey(getUri(uri).absoluteString) !== null;
111+
isInBitmapMemoryCache(key: string): boolean {
112+
return this.mIos.imageFromMemoryCacheForKey(key) !== null;
113113
}
114114

115-
evictFromMemoryCache(uri: string): void {
116-
this.mIos.removeImageFromMemoryForKey(getUri(uri).absoluteString);
115+
evictFromMemoryCache(key: string): void {
116+
this.mIos.removeImageFromMemoryForKey(key);
117117
}
118118

119-
evictFromDiskCache(uri: string): void {
120-
this.mIos.removeImageFromDiskForKey(getUri(uri).absoluteString);
119+
evictFromDiskCache(key: string): void {
120+
this.mIos.removeImageFromDiskForKey(key);
121121
}
122122

123-
evictFromCache(uri: string): void {
124-
const key = getUri(uri).absoluteString;
123+
evictFromCache(key: string): void {
124+
// const key = getUri(uri).absoluteString;
125125
this.mIos.removeImageFromDiskForKey(key);
126126
this.mIos.removeImageFromMemoryForKey(key);
127127
// this.mIos.removeImageForKeyWithCompletion(getUri(uri).absoluteString, null);
@@ -222,6 +222,11 @@ export class Img extends ImageBase {
222222
//@ts-ignore
223223
nativeImageViewProtected: SDAnimatedImageView | UIImageView;
224224
isLoading = false;
225+
mCacheKey:string
226+
227+
get cacheKey() {
228+
return this.mCacheKey
229+
}
225230
protected mImageSourceAffectsLayout: boolean = true;
226231
protected mCIFilter: CIFilter;
227232
public createNativeView() {
@@ -278,13 +283,15 @@ export class Img extends ImageBase {
278283
const imagePipeLine = getImagePipeline();
279284
const src = this.src;
280285
if (!(src instanceof ImageSource)) {
281-
const uri = getUri(src).absoluteString;
282-
const isInCache = imagePipeLine.isInBitmapMemoryCache(uri);
283-
if (isInCache) {
284-
imagePipeLine.evictFromCache(uri);
285-
}
286+
const cachekKey = this.mCacheKey || getUri(src).absoluteString;
287+
// const isInCache = imagePipeLine.isInBitmapMemoryCache(cachekKey);
288+
// if (isInCache) {
289+
imagePipeLine.evictFromCache(cachekKey);
290+
// }
286291
}
287292
this.src = null;
293+
// ensure we clear the image as
294+
this._setNativeImage(null, false);
288295
this.src = src;
289296
}
290297

@@ -315,9 +322,8 @@ export class Img extends ImageBase {
315322
} else {
316323
this.nativeImageViewProtected.image = nativeImage;
317324
}
318-
319325
if (this.mImageSourceAffectsLayout) {
320-
this.mImageSourceAffectsLayout = false;
326+
// this.mImageSourceAffectsLayout = false;
321327
this.requestLayout();
322328
}
323329
}
@@ -463,6 +469,7 @@ export class Img extends ImageBase {
463469
}
464470
context.setValueForKey(SDImagePipelineTransformer.transformerWithTransformers(transformers), SDWebImageContextImageTransformer);
465471
}
472+
this.mCacheKey = SDWebImageManager.sharedManager.cacheKeyForURLContext(uri, context)
466473
this.nativeImageViewProtected.sd_setImageWithURLPlaceholderImageOptionsContextProgressCompleted(
467474
uri,
468475
this.placeholderImage,

0 commit comments

Comments
 (0)