mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-24 07:05:35 +00:00
Add support for attempting to get image synchronously
This commit is contained in:
@@ -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];
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user