Merge pull request #1606 from wendylu/render_fix

[ASNetworkImageNode] Fix threading issue in current image quality
This commit is contained in:
appleguy 2016-05-02 18:02:29 -07:00
commit a9e742c92f

View File

@ -122,10 +122,14 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
_URL = URL; _URL = URL;
if (reset || _URL == nil) { BOOL hasURL = _URL == nil;
if (reset || hasURL) {
self.image = _defaultImage; self.image = _defaultImage;
ASPerformBlockOnMainThread(^{ /* We want to maintain the order that currentImageQuality is set regardless of the calling thread,
self.currentImageQuality = 1.0; so always use a dispatch_async to ensure that we queue the operations in the correct order.
(see comment in displayDidFinish) */
dispatch_async(dispatch_get_main_queue(), ^{
self.currentImageQuality = hasURL ? 0.0 : 1.0;
}); });
} }
@ -151,8 +155,12 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
_defaultImage = defaultImage; _defaultImage = defaultImage;
if (!_imageLoaded) { if (!_imageLoaded) {
ASPerformBlockOnMainThread(^{ BOOL hasURL = _URL == nil;
self.currentImageQuality = 0.0; /* We want to maintain the order that currentImageQuality is set regardless of the calling thread,
so always use a dispatch_async to ensure that we queue the operations in the correct order.
(see comment in displayDidFinish) */
dispatch_async(dispatch_get_main_queue(), ^{
self.currentImageQuality = hasURL ? 0.0 : 1.0;
}); });
_lock.unlock(); _lock.unlock();
// Locking: it is important to release _lock before entering setImage:, as it needs to release the lock before -invalidateCalculatedLayout. // Locking: it is important to release _lock before entering setImage:, as it needs to release the lock before -invalidateCalculatedLayout.
@ -229,7 +237,9 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
if (result) { if (result) {
self.image = result; self.image = result;
_imageLoaded = YES; _imageLoaded = YES;
dispatch_async(dispatch_get_main_queue(), ^{
_currentImageQuality = 1.0; _currentImageQuality = 1.0;
});
} }
} }
} }
@ -322,7 +332,7 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
return; return;
} }
strongSelf.image = progressImage; strongSelf.image = progressImage;
ASPerformBlockOnMainThread(^{ dispatch_async(dispatch_get_main_queue(), ^{
strongSelf->_currentImageQuality = progress; strongSelf->_currentImageQuality = progress;
}); });
}; };
@ -347,7 +357,7 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
self.animatedImage = nil; self.animatedImage = nil;
self.image = _defaultImage; self.image = _defaultImage;
_imageLoaded = NO; _imageLoaded = NO;
ASPerformBlockOnMainThread(^{ dispatch_async(dispatch_get_main_queue(), ^{
self.currentImageQuality = 0.0; self.currentImageQuality = 0.0;
}); });
} }
@ -433,7 +443,12 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
} }
_imageLoaded = YES; _imageLoaded = YES;
/* We want to maintain the order that currentImageQuality is set regardless of the calling thread,
so always use a dispatch_async to ensure that we queue the operations in the correct order.
(see comment in displayDidFinish) */
dispatch_async(dispatch_get_main_queue(), ^{
self.currentImageQuality = 1.0; self.currentImageQuality = 1.0;
});
[_delegate imageNode:self didLoadImage:self.image]; [_delegate imageNode:self didLoadImage:self.image];
}); });
} }
@ -459,7 +474,9 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
} else { } else {
strongSelf.image = [imageContainer asdk_image]; strongSelf.image = [imageContainer asdk_image];
} }
dispatch_async(dispatch_get_main_queue(), ^{
strongSelf->_currentImageQuality = 1.0; strongSelf->_currentImageQuality = 1.0;
});
} }
strongSelf->_downloadIdentifier = nil; strongSelf->_downloadIdentifier = nil;