Merge pull request #3327 from soondl/fix/imageView_blink_diskDataSync
Fix imageView blinks with option 'SDImageCacheQueryDiskDataSync'
This commit is contained in:
commit
020481d5c8
|
@ -552,16 +552,19 @@ static NSString * _defaultDiskCacheDirectory;
|
||||||
// 2. in-memory cache miss & diskDataSync
|
// 2. in-memory cache miss & diskDataSync
|
||||||
BOOL shouldQueryDiskSync = ((image && options & SDImageCacheQueryMemoryDataSync) ||
|
BOOL shouldQueryDiskSync = ((image && options & SDImageCacheQueryMemoryDataSync) ||
|
||||||
(!image && options & SDImageCacheQueryDiskDataSync));
|
(!image && options & SDImageCacheQueryDiskDataSync));
|
||||||
void(^queryDiskBlock)(void) = ^{
|
NSData* (^queryDiskDataBlock)(void) = ^NSData* {
|
||||||
if (operation.isCancelled) {
|
if (operation.isCancelled) {
|
||||||
if (doneBlock) {
|
return nil;
|
||||||
doneBlock(nil, nil, SDImageCacheTypeNone);
|
}
|
||||||
}
|
|
||||||
return;
|
return [self diskImageDataBySearchingAllPathsForKey:key];
|
||||||
|
};
|
||||||
|
|
||||||
|
UIImage* (^queryDiskImageBlock)(NSData*) = ^UIImage*(NSData* diskData) {
|
||||||
|
if (operation.isCancelled) {
|
||||||
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
@autoreleasepool {
|
|
||||||
NSData *diskData = [self diskImageDataBySearchingAllPathsForKey:key];
|
|
||||||
UIImage *diskImage;
|
UIImage *diskImage;
|
||||||
if (image) {
|
if (image) {
|
||||||
// the image is from in-memory cache, but need image data
|
// the image is from in-memory cache, but need image data
|
||||||
|
@ -579,24 +582,30 @@ static NSString * _defaultDiskCacheDirectory;
|
||||||
[self.memoryCache setObject:diskImage forKey:key cost:cost];
|
[self.memoryCache setObject:diskImage forKey:key cost:cost];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return diskImage;
|
||||||
if (doneBlock) {
|
|
||||||
if (shouldQueryDiskSync) {
|
|
||||||
doneBlock(diskImage, diskData, SDImageCacheTypeDisk);
|
|
||||||
} else {
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
|
||||||
doneBlock(diskImage, diskData, SDImageCacheTypeDisk);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Query in ioQueue to keep IO-safe
|
// Query in ioQueue to keep IO-safe
|
||||||
if (shouldQueryDiskSync) {
|
if (shouldQueryDiskSync) {
|
||||||
dispatch_sync(self.ioQueue, queryDiskBlock);
|
__block NSData* diskData;
|
||||||
|
__block UIImage* diskImage;
|
||||||
|
dispatch_sync(self.ioQueue, ^{
|
||||||
|
diskData = queryDiskDataBlock();
|
||||||
|
diskImage = queryDiskImageBlock(diskData);
|
||||||
|
});
|
||||||
|
if (doneBlock) {
|
||||||
|
doneBlock(diskImage, diskData, SDImageCacheTypeDisk);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
dispatch_async(self.ioQueue, queryDiskBlock);
|
dispatch_async(self.ioQueue, ^{
|
||||||
|
NSData* diskData = queryDiskDataBlock();
|
||||||
|
UIImage* diskImage = queryDiskImageBlock(diskData);
|
||||||
|
if (doneBlock) {
|
||||||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
|
doneBlock(diskImage, diskData, SDImageCacheTypeDisk);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return operation;
|
return operation;
|
||||||
|
|
|
@ -35,6 +35,30 @@
|
||||||
[self waitForExpectationsWithCommonTimeout];
|
[self waitForExpectationsWithCommonTimeout];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)testUIImageViewSetImageWithURLDiskSync {
|
||||||
|
NSData *imageData = [NSData dataWithContentsOfFile:[self testJPEGPath]];
|
||||||
|
|
||||||
|
// Ensure the image is cached in disk but not memory
|
||||||
|
[SDImageCache.sharedImageCache removeImageFromMemoryForKey:kTestJPEGURL];
|
||||||
|
[SDImageCache.sharedImageCache removeImageFromDiskForKey:kTestJPEGURL];
|
||||||
|
[SDImageCache.sharedImageCache storeImageDataToDisk:imageData forKey:kTestJPEGURL];
|
||||||
|
|
||||||
|
UIImageView *imageView = [[UIImageView alloc] init];
|
||||||
|
NSURL *originalImageURL = [NSURL URLWithString:kTestJPEGURL];
|
||||||
|
|
||||||
|
[imageView sd_setImageWithURL:originalImageURL
|
||||||
|
placeholderImage:nil
|
||||||
|
options:SDWebImageQueryDiskDataSync
|
||||||
|
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
|
||||||
|
expect(image).toNot.beNil();
|
||||||
|
expect(error).to.beNil();
|
||||||
|
expect(originalImageURL).to.equal(imageURL);
|
||||||
|
expect(imageView.image).to.equal(image);
|
||||||
|
}];
|
||||||
|
expect(imageView.sd_imageURL).equal(originalImageURL);
|
||||||
|
expect(imageView.image).toNot.beNil();
|
||||||
|
}
|
||||||
|
|
||||||
#if SD_UIKIT
|
#if SD_UIKIT
|
||||||
- (void)testUIImageViewSetHighlightedImageWithURL {
|
- (void)testUIImageViewSetHighlightedImageWithURL {
|
||||||
XCTestExpectation *expectation = [self expectationWithDescription:@"UIImageView setHighlightedImageWithURL"];
|
XCTestExpectation *expectation = [self expectationWithDescription:@"UIImageView setHighlightedImageWithURL"];
|
||||||
|
|
Loading…
Reference in New Issue