Add support for attempting to get image synchronously

This commit is contained in:
Garrett Moon
2016-03-11 15:51:17 -08:00
parent a4e3ba49b4
commit af61645faf
3 changed files with 35 additions and 0 deletions

View File

@@ -49,6 +49,7 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
BOOL _cacheSupportsNewProtocol;
BOOL _cacheSupportsClearing;
BOOL _cacheSupportsSynchronousFetch;
}
@end
@@ -73,6 +74,7 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
_cacheSupportsNewProtocol = [cache respondsToSelector:@selector(cachedImageWithURL:callbackQueue:completion:)];
_cacheSupportsClearing = [cache respondsToSelector:@selector(clearFetchedImageFromCacheWithURL:)];
_cacheSupportsSynchronousFetch = [cache respondsToSelector:@selector(synchronouslyFetchedCachedImageWithURL:)];
_shouldCacheImage = YES;
self.shouldBypassEnsureDisplay = YES;
@@ -169,6 +171,19 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
- (void)displayWillStart
{
[super displayWillStart];
if (_cacheSupportsSynchronousFetch) {
{
ASDN::MutexLocker l(_lock);
if (_URL && _downloadIdentifier == nil) {
UIImage *result = [_cache synchronouslyFetchedCachedImageWithURL:_URL];
if (result) {
self.image = result;
_imageLoaded = YES;
}
}
}
}
[self fetchData];

View File

@@ -17,6 +17,19 @@ typedef void(^ASImageCacherCompletion)(UIImage * _Nullable imageFromCache);
@optional
/**
@abstract Attempts to fetch an image with the given URL from a memory cache.
@param URL The URL of the image to retrieve from the cache.
@discussion This method exists to support synchronous rendering of nodes. Before the layer is drawn, this method
is called to attempt to get the image out of the cache synchronously. This allows drawing to occur on the main thread
if displaysAsynchronously is set to NO or recursivelyEnsureDisplaySynchronously: has been called.
If `URL` is nil, `completion` will be invoked immediately with a nil image. This method *should* block
the calling thread to fetch the image from a fast memory cache. It is OK to return nil from this method and instead
support only cachedImageWithURL:callbackQueue:completion: however, synchronous rendering will not be possible.
*/
- (_Nullable UIImage *)synchronouslyFetchedCachedImageWithURL:(NSURL *)URL;
/**
@abstract Attempts to fetch an image with the given URL from the cache.
@param URL The URL of the image to retrieve from the cache.

View File

@@ -29,6 +29,13 @@
#pragma mark ASImageProtocols
- (UIImage *)synchronouslyFetchedCachedImageWithURL:(NSURL *)URL
{
NSString *key = [[PINRemoteImageManager sharedImageManager] cacheKeyForURL:URL processorKey:nil];
PINRemoteImageManagerResult *result = [[PINRemoteImageManager sharedImageManager] synchronousImageFromCacheWithCacheKey:key options:PINRemoteImageManagerDownloadOptionsSkipDecode];
return result.image;
}
- (void)fetchCachedImageWithURL:(NSURL *)URL
callbackQueue:(dispatch_queue_t)callbackQueue
completion:(void (^)(CGImageRef imageFromCache))completion