@@ -20,6 +20,7 @@ @interface SDWebImageDownloader ()
2020
2121@property (strong , nonatomic ) NSOperationQueue *downloadQueue;
2222@property (weak , nonatomic ) NSOperation *lastAddedOperation;
23+ @property (assign , nonatomic ) Class operationClass;
2324@property (strong , nonatomic ) NSMutableDictionary *URLCallbacks;
2425@property (strong , nonatomic ) NSMutableDictionary *HTTPHeaders;
2526// This queue is used to serialize the handling of the network responses of all the download operation in a single queue
@@ -63,6 +64,7 @@ + (SDWebImageDownloader *)sharedDownloader {
6364
6465- (id )init {
6566 if ((self = [super init ])) {
67+ _operationClass = [SDWebImageDownloaderOperation class ];
6668 _executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
6769 _downloadQueue = [NSOperationQueue new ];
6870 _downloadQueue.maxConcurrentOperationCount = 2 ;
@@ -104,6 +106,10 @@ - (NSInteger)maxConcurrentDownloads {
104106 return _downloadQueue.maxConcurrentOperationCount ;
105107}
106108
109+ - (void )setOperationClass : (Class )operationClass {
110+ _operationClass = operationClass ?: [SDWebImageDownloaderOperation class ];
111+ }
112+
107113- (id <SDWebImageOperation>)downloadImageWithURL : (NSURL *)url options : (SDWebImageDownloaderOptions)options progress : (SDWebImageDownloaderProgressBlock)progressBlock completed : (SDWebImageDownloaderCompletedBlock)completedBlock {
108114 __block SDWebImageDownloaderOperation *operation;
109115 __weak SDWebImageDownloader *wself = self;
@@ -124,34 +130,34 @@ - (NSInteger)maxConcurrentDownloads {
124130 else {
125131 request.allHTTPHeaderFields = wself.HTTPHeaders ;
126132 }
127- operation = [[SDWebImageDownloaderOperation alloc ] initWithRequest: request
128- options: options
129- progress: ^(NSInteger receivedSize, NSInteger expectedSize) {
130- SDWebImageDownloader *sself = wself;
131- if (!sself) return ;
132- NSArray *callbacksForURL = [sself callbacksForURL: url];
133- for (NSDictionary *callbacks in callbacksForURL) {
134- SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey ];
135- if (callback) callback (receivedSize, expectedSize);
136- }
137- }
138- completed: ^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
139- SDWebImageDownloader *sself = wself;
140- if (!sself) return ;
141- NSArray *callbacksForURL = [sself callbacksForURL: url];
142- if (finished) {
143- [sself removeCallbacksForURL: url];
144- }
145- for (NSDictionary *callbacks in callbacksForURL) {
146- SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey ];
147- if (callback) callback (image, data, error, finished);
148- }
149- }
150- cancelled: ^{
151- SDWebImageDownloader *sself = wself;
152- if (!sself) return ;
153- [sself removeCallbacksForURL: url];
154- }];
133+ operation = [[wself.operationClass alloc ] initWithRequest: request
134+ options: options
135+ progress: ^(NSInteger receivedSize, NSInteger expectedSize) {
136+ SDWebImageDownloader *sself = wself;
137+ if (!sself) return ;
138+ NSArray *callbacksForURL = [sself callbacksForURL: url];
139+ for (NSDictionary *callbacks in callbacksForURL) {
140+ SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey ];
141+ if (callback) callback (receivedSize, expectedSize);
142+ }
143+ }
144+ completed: ^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
145+ SDWebImageDownloader *sself = wself;
146+ if (!sself) return ;
147+ NSArray *callbacksForURL = [sself callbacksForURL: url];
148+ if (finished) {
149+ [sself removeCallbacksForURL: url];
150+ }
151+ for (NSDictionary *callbacks in callbacksForURL) {
152+ SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey ];
153+ if (callback) callback (image, data, error, finished);
154+ }
155+ }
156+ cancelled: ^{
157+ SDWebImageDownloader *sself = wself;
158+ if (!sself) return ;
159+ [sself removeCallbacksForURL: url];
160+ }];
155161
156162 if (wself.username && wself.password ) {
157163 operation.credential = [NSURLCredential credentialWithUser: wself.username password: wself.password persistence: NSURLCredentialPersistenceForSession];
0 commit comments