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,31 +95,34 @@ @@ -94,31 +95,34 @@
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];
106 } 110 }
107 111
108 -- (void)setFinished:(BOOL)finished 112 +- (void)reset
109 { 113 {
110 - [self willChangeValueForKey:@"isFinished"];  
111 - _finished = finished;  
112 - [self didChangeValueForKey:@"isFinished"];  
113 -  
114 - if (finished)  
115 - {  
116 self.cancelBlock = nil; 114 self.cancelBlock = nil;
117 self.completedBlock = nil; 115 self.completedBlock = nil;
118 self.progressBlock = nil; 116 self.progressBlock = nil;
119 self.connection = nil; 117 self.connection = nil;
120 self.imageData = nil; 118 self.imageData = nil;
121 - } 119 +}
  120 +
  121 +- (void)setFinished:(BOOL)finished
  122 +{
  123 + [self willChangeValueForKey:@"isFinished"];
  124 + _finished = finished;
  125 + [self didChangeValueForKey:@"isFinished"];
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];