@@ -713,55 +713,67 @@ - (void)_lazilyLoadImageIfNecessary
713713 } else {
714714 __weak __typeof__ (self) weakSelf = self;
715715 auto finished = ^(id <ASImageContainerProtocol>imageContainer, NSError *error, id downloadIdentifier, ASNetworkImageSource imageSource) {
716-
717- __typeof__ (self) strongSelf = weakSelf;
718- if (strongSelf == nil ) {
719- return ;
720- }
721-
722- as_log_verbose (ASImageLoadingLog (), " Downloaded image for %@ img: %@ urls: %@" , self, [imageContainer asdk_image ], URLs);
723-
724- // Grab the lock for the rest of the block
725- ASDN::MutexLocker l (strongSelf->__instanceLock__ );
726-
727- // Getting a result back for a different download identifier, download must not have been successfully canceled
728- if (ASObjectIsEqual (strongSelf->_downloadIdentifier , downloadIdentifier) == NO && downloadIdentifier != nil ) {
729- return ;
730- }
716+ ASPerformBlockOnBackgroundThread (^{
717+ __typeof__ (self) strongSelf = weakSelf;
718+ if (strongSelf == nil ) {
719+ return ;
720+ }
731721
732- // No longer in preload range, no point in setting the results (they won't be cleared in exit preload range)
733- if (ASInterfaceStateIncludesPreload (self->_interfaceState ) == NO ) {
734- return ;
735- }
736-
737- if (imageContainer != nil ) {
738- [strongSelf _locked_setCurrentImageQuality: 1.0 ];
739- if ([imageContainer asdk_animatedImageData ] && strongSelf->_downloaderFlags .downloaderImplementsAnimatedImage ) {
740- id animatedImage = [strongSelf->_downloader animatedImageWithData: [imageContainer asdk_animatedImageData ]];
741- [strongSelf _locked_setAnimatedImage: animatedImage];
742- } else {
743- [strongSelf _locked__setImage: [imageContainer asdk_image ]];
722+ as_log_verbose (ASImageLoadingLog (), " Downloaded image for %@ img: %@ urls: %@" , self, [imageContainer asdk_image ], URLs);
723+
724+ // Grab the lock for the rest of the block
725+ ASDN::MutexLocker l (strongSelf->__instanceLock__ );
726+
727+ // Getting a result back for a different download identifier, download must not have been successfully canceled
728+ if (ASObjectIsEqual (strongSelf->_downloadIdentifier , downloadIdentifier) == NO && downloadIdentifier != nil ) {
729+ return ;
744730 }
745- strongSelf->_imageLoaded = YES ;
746- }
747-
748- strongSelf->_downloadIdentifier = nil ;
749- strongSelf->_cacheUUID = nil ;
750-
751- if (imageContainer != nil ) {
752- if (strongSelf->_delegateFlags .delegateDidLoadImageWithInfo ) {
753- ASDN::MutexUnlocker u (strongSelf->__instanceLock__ );
754- ASNetworkImageNodeDidLoadInfo info = {};
755- info.imageSource = imageSource;
756- [delegate imageNode: strongSelf didLoadImage: strongSelf.image info: info];
757- } else if (strongSelf->_delegateFlags .delegateDidLoadImage ) {
758- ASDN::MutexUnlocker u (strongSelf->__instanceLock__ );
759- [delegate imageNode: strongSelf didLoadImage: strongSelf.image];
731+
732+ // No longer in preload range, no point in setting the results (they won't be cleared in exit preload range)
733+ if (ASInterfaceStateIncludesPreload (self->_interfaceState ) == NO ) {
734+ return ;
760735 }
761- } else if (error && strongSelf->_delegateFlags .delegateDidFailWithError ) {
762- ASDN::MutexUnlocker u (strongSelf->__instanceLock__ );
763- [delegate imageNode: strongSelf didFailWithError: error];
764- }
736+
737+ if (imageContainer != nil ) {
738+ [strongSelf _locked_setCurrentImageQuality: 1.0 ];
739+ NSData *animatedImageData = [imageContainer asdk_animatedImageData ];
740+ if (animatedImageData && strongSelf->_downloaderFlags .downloaderImplementsAnimatedImage ) {
741+ id animatedImage = [strongSelf->_downloader animatedImageWithData: animatedImageData];
742+ [strongSelf _locked_setAnimatedImage: animatedImage];
743+ } else {
744+ [strongSelf _locked__setImage: [imageContainer asdk_image ]];
745+ }
746+ strongSelf->_imageLoaded = YES ;
747+ }
748+
749+ strongSelf->_downloadIdentifier = nil ;
750+ strongSelf->_cacheUUID = nil ;
751+
752+ ASPerformBlockOnMainThread (^{
753+ __typeof__ (self) strongSelf = weakSelf;
754+ if (strongSelf == nil ) {
755+ return ;
756+ }
757+
758+ // Grab the lock for the rest of the block
759+ ASDN::MutexLocker l (strongSelf->__instanceLock__ );
760+
761+ if (imageContainer != nil ) {
762+ if (strongSelf->_delegateFlags .delegateDidLoadImageWithInfo ) {
763+ ASDN::MutexUnlocker u (strongSelf->__instanceLock__ );
764+ ASNetworkImageNodeDidLoadInfo info = {};
765+ info.imageSource = imageSource;
766+ [delegate imageNode: strongSelf didLoadImage: strongSelf.image info: info];
767+ } else if (strongSelf->_delegateFlags .delegateDidLoadImage ) {
768+ ASDN::MutexUnlocker u (strongSelf->__instanceLock__ );
769+ [delegate imageNode: strongSelf didLoadImage: strongSelf.image];
770+ }
771+ } else if (error && strongSelf->_delegateFlags .delegateDidFailWithError ) {
772+ ASDN::MutexUnlocker u (strongSelf->__instanceLock__ );
773+ [delegate imageNode: strongSelf didFailWithError: error];
774+ }
775+ });
776+ });
765777 };
766778
767779 // As the _cache and _downloader is only set once in the intializer we don't have to use a
0 commit comments