YYWebImageManager.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // YYWebImageManager.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 15/2/19.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "YYWebImageManager.h"
  12. #import "YYImageCache.h"
  13. #import "YYWebImageOperation.h"
  14. #import "YYImageCoder.h"
  15. @implementation YYWebImageManager
  16. + (instancetype)sharedManager {
  17. static YYWebImageManager *manager;
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. YYImageCache *cache = [YYImageCache sharedCache];
  21. NSOperationQueue *queue = [NSOperationQueue new];
  22. if ([queue respondsToSelector:@selector(setQualityOfService:)]) {
  23. queue.qualityOfService = NSQualityOfServiceBackground;
  24. }
  25. manager = [[self alloc] initWithCache:cache queue:queue];
  26. });
  27. return manager;
  28. }
  29. - (instancetype)init {
  30. @throw [NSException exceptionWithName:@"YYWebImageManager init error" reason:@"Use the designated initializer to init." userInfo:nil];
  31. return [self initWithCache:nil queue:nil];
  32. }
  33. - (instancetype)initWithCache:(YYImageCache *)cache queue:(NSOperationQueue *)queue{
  34. self = [super init];
  35. if (!self) return nil;
  36. _cache = cache;
  37. _queue = queue;
  38. _timeout = 15.0;
  39. if (YYImageWebPAvailable()) {
  40. _headers = @{ @"Accept" : @"image/webp,image/*;q=0.8" };
  41. } else {
  42. _headers = @{ @"Accept" : @"image/*;q=0.8" };
  43. }
  44. return self;
  45. }
  46. - (YYWebImageOperation *)requestImageWithURL:(NSURL *)url
  47. options:(YYWebImageOptions)options
  48. progress:(YYWebImageProgressBlock)progress
  49. transform:(YYWebImageTransformBlock)transform
  50. completion:(YYWebImageCompletionBlock)completion {
  51. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  52. request.timeoutInterval = _timeout;
  53. request.HTTPShouldHandleCookies = (options & YYWebImageOptionHandleCookies) != 0;
  54. request.allHTTPHeaderFields = [self headersForURL:url];
  55. request.HTTPShouldUsePipelining = YES;
  56. request.cachePolicy = (options & YYWebImageOptionUseNSURLCache) ?
  57. NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData;
  58. YYWebImageOperation *operation = [[YYWebImageOperation alloc] initWithRequest:request
  59. options:options
  60. cache:_cache
  61. cacheKey:[self cacheKeyForURL:url]
  62. progress:progress
  63. transform:transform ? transform : _sharedTransformBlock
  64. completion:completion];
  65. if (_username && _password) {
  66. operation.credential = [NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceForSession];
  67. }
  68. if (operation) {
  69. NSOperationQueue *queue = _queue;
  70. if (queue) {
  71. [queue addOperation:operation];
  72. } else {
  73. [operation start];
  74. }
  75. }
  76. return operation;
  77. }
  78. - (NSDictionary *)headersForURL:(NSURL *)url {
  79. if (!url) return nil;
  80. return _headersFilter ? _headersFilter(url, _headers) : _headers;
  81. }
  82. - (NSString *)cacheKeyForURL:(NSURL *)url {
  83. if (!url) return nil;
  84. return _cacheKeyFilter ? _cacheKeyFilter(url) : url.absoluteString;
  85. }
  86. @end