Merge pull request #2468 from zhongwuzw/optimize-sdanimatedimage

Decrease animated decode times when cache enable
This commit is contained in:
DreamPiggy 2018-09-05 10:55:00 +08:00 committed by GitHub
commit 0f14258e83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 5 deletions

View File

@ -637,9 +637,9 @@ static NSUInteger SDDeviceFreeMemory() {
// If current data is the same data (or instance) as previous data
self.isProgressive = YES;
} else if (currentData.length > previousData.length) {
// If current data is appended by previous data, use `NSDataSearchAnchored`
// If current data is appended by previous data, use `NSDataSearchAnchored`, search is limited to start of currentData
NSRange range = [currentData rangeOfData:previousData options:NSDataSearchAnchored range:NSMakeRange(0, previousData.length)];
if (range.location == 0 && range.length == previousData.length) {
if (range.location != NSNotFound) {
// Contains hole previous data and they start with the same beginning
self.isProgressive = YES;
}
@ -689,8 +689,10 @@ static NSUInteger SDDeviceFreeMemory() {
// Update the current frame
UIImage *currentFrame;
UIImage *fetchFrame;
LOCKBLOCK({
currentFrame = self.frameBuffer[@(currentFrameIndex)];
fetchFrame = currentFrame ? self.frameBuffer[@(nextFrameIndex)] : nil;
});
BOOL bufferFull = NO;
if (currentFrame) {
@ -743,13 +745,14 @@ static NSUInteger SDDeviceFreeMemory() {
// Or, most cases, the decode speed is faster than render speed, we fetch next frame
fetchFrameIndex = nextFrameIndex;
}
if (!bufferFull && self.fetchQueue.operationCount == 0) {
if (!fetchFrame && !bufferFull && self.fetchQueue.operationCount == 0) {
// Prefetch next frame in background queue
UIImage<SDAnimatedImage> *animatedImage = self.animatedImage;
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
UIImage *fetchFrame = [animatedImage animatedImageFrameAtIndex:fetchFrameIndex];
UIImage *frame = [animatedImage animatedImageFrameAtIndex:fetchFrameIndex];
LOCKBLOCK({
self.frameBuffer[@(fetchFrameIndex)] = fetchFrame;
self.frameBuffer[@(fetchFrameIndex)] = frame;
});
}];
[self.fetchQueue addOperation:operation];