Authored by Olivier Poitrey

Ensure we're not decoding the image in the main thread

@@ -50,6 +50,7 @@ @@ -50,6 +50,7 @@
50 if (self.isCancelled) 50 if (self.isCancelled)
51 { 51 {
52 self.finished = YES; 52 self.finished = YES;
  53 + [self reset];
53 return; 54 return;
54 } 55 }
55 56
@@ -94,15 +95,27 @@ @@ -94,15 +95,27 @@
94 95
95 // As we cancelled the connection, its callback won't be called and thus won't 96 // As we cancelled the connection, its callback won't be called and thus won't
96 // maintain the isFinished and isExecuting flags. 97 // maintain the isFinished and isExecuting flags.
97 - if (!self.isFinished) self.finished = YES;  
98 if (self.isExecuting) self.executing = NO; 98 if (self.isExecuting) self.executing = NO;
  99 + if (!self.isFinished) self.finished = YES;
99 } 100 }
  101 +
  102 + [self reset];
100 } 103 }
101 104
102 - (void)done 105 - (void)done
103 { 106 {
104 self.finished = YES; 107 self.finished = YES;
105 self.executing = NO; 108 self.executing = NO;
  109 + [self reset];
  110 +}
  111 +
  112 +- (void)reset
  113 +{
  114 + self.cancelBlock = nil;
  115 + self.completedBlock = nil;
  116 + self.progressBlock = nil;
  117 + self.connection = nil;
  118 + self.imageData = nil;
106 } 119 }
107 120
108 - (void)setFinished:(BOOL)finished 121 - (void)setFinished:(BOOL)finished
@@ -110,15 +123,6 @@ @@ -110,15 +123,6 @@
110 [self willChangeValueForKey:@"isFinished"]; 123 [self willChangeValueForKey:@"isFinished"];
111 _finished = finished; 124 _finished = finished;
112 [self didChangeValueForKey:@"isFinished"]; 125 [self didChangeValueForKey:@"isFinished"];
113 -  
114 - if (finished)  
115 - {  
116 - self.cancelBlock = nil;  
117 - self.completedBlock = nil;  
118 - self.progressBlock = nil;  
119 - self.connection = nil;  
120 - self.imageData = nil;  
121 - }  
122 } 126 }
123 127
124 - (void)setExecuting:(BOOL)executing 128 - (void)setExecuting:(BOOL)executing
@@ -234,8 +238,17 @@ @@ -234,8 +238,17 @@
234 238
235 if (self.completedBlock) 239 if (self.completedBlock)
236 { 240 {
237 - UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, self.imageData)];  
238 - self.completedBlock(image, nil, YES); 241 + __block SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock;
  242 + UIImage *image = SDScaledImageForPath(self.request.URL.absoluteString, self.imageData);
  243 + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^
  244 + {
  245 + UIImage *decodedImage = [UIImage decodedImageWithImage:image];
  246 + dispatch_async(dispatch_get_main_queue(), ^
  247 + {
  248 + completionBlock(decodedImage, nil, YES);
  249 + completionBlock = nil;
  250 + });
  251 + });
239 } 252 }
240 253
241 [self done]; 254 [self done];