Let ASMultiplexImageNode skip failed image identifiers

This commit is contained in:
andyscott
2015-01-15 14:54:40 -08:00
parent 63e97f0901
commit 60b27829e1
2 changed files with 23 additions and 8 deletions

View File

@@ -62,11 +62,13 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent
// Image flags.
BOOL _downloadsIntermediateImages; // Defaults to NO.
BOOL _haltLoadingOnError; // Defaults to NO.
OSSpinLock _imageIdentifiersLock;
NSArray *_imageIdentifiers;
id _loadedImageIdentifier;
id _loadingImageIdentifier;
id _displayedImageIdentifier;
id _failedImageIdentifier; // Reset to nil whenever an image is successfully loaded
// Networking.
id _downloadIdentifier;
@@ -353,9 +355,11 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent
{
OSSpinLockLock(&_imageIdentifiersLock);
id _currentImageIdentifier = _failedImageIdentifier != nil ? _failedImageIdentifier : _loadedImageIdentifier;
// If we've already loaded the best identifier, we've got nothing else to do.
id bestImageIdentifier = ([_imageIdentifiers count] > 0) ? _imageIdentifiers[0] : nil;
if (!bestImageIdentifier || [_loadedImageIdentifier isEqual:bestImageIdentifier]) {
if (!bestImageIdentifier || [_currentImageIdentifier isEqual:bestImageIdentifier]) {
OSSpinLockUnlock(&_imageIdentifiersLock);
return nil;
}
@@ -368,15 +372,15 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent
}
// Otherwise, load progressively.
else {
NSUInteger loadedIndex = [_imageIdentifiers indexOfObject:_loadedImageIdentifier];
NSUInteger currentIndex = [_imageIdentifiers indexOfObject:_currentImageIdentifier];
// If nothing has loaded yet, load the worst identifier.
if (loadedIndex == NSNotFound) {
if (currentIndex == NSNotFound) {
nextImageIdentifierToDownload = [_imageIdentifiers lastObject];
}
// Otherwise, load the next best identifier (if there is one)
else if (loadedIndex > 0) {
nextImageIdentifierToDownload = _imageIdentifiers[loadedIndex - 1];
else if (currentIndex > 0) {
nextImageIdentifierToDownload = _imageIdentifiers[currentIndex - 1];
}
}
@@ -588,10 +592,16 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent
#pragma mark -
- (void)_finishedLoadingImage:(UIImage *)image forIdentifier:(id)imageIdentifier error:(NSError *)error
{
// If we failed to load, we stop the loading process.
// If we failed to load and _haltLoadingOnError is YES, we stop the loading process.
// Note that if we bailed before we began downloading because the best identifier changed, we don't bail, but rather just begin loading the best image identifier.
if (error && error.code != ASMultiplexImageNodeErrorCodeBestImageIdentifierChanged)
return;
if (error && error.code != ASMultiplexImageNodeErrorCodeBestImageIdentifierChanged) {
if (_haltLoadingOnError) {
return;
}
_failedImageIdentifier = imageIdentifier;
} else {
_failedImageIdentifier = nil;
}
OSSpinLockLock(&_imageIdentifiersLock);
NSUInteger imageIdentifierCount = [_imageIdentifiers count];