Authored by Hao Lian
Committed by Hao Lian

SDWebImageDownloaderOperation: pass orientation to initWithCGImage during progressive rendering

@@ -32,6 +32,7 @@ @@ -32,6 +32,7 @@
32 32
33 @implementation SDWebImageDownloaderOperation { 33 @implementation SDWebImageDownloaderOperation {
34 size_t width, height; 34 size_t width, height;
  35 + UIImageOrientation orientation;
35 BOOL responseFromCached; 36 BOOL responseFromCached;
36 } 37 }
37 38
@@ -216,12 +217,22 @@ @@ -216,12 +217,22 @@
216 if (width + height == 0) { 217 if (width + height == 0) {
217 CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL); 218 CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
218 if (properties) { 219 if (properties) {
  220 + NSInteger orientationValue = -1;
219 CFTypeRef val = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight); 221 CFTypeRef val = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
220 if (val) CFNumberGetValue(val, kCFNumberLongType, &height); 222 if (val) CFNumberGetValue(val, kCFNumberLongType, &height);
221 val = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth); 223 val = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
222 if (val) CFNumberGetValue(val, kCFNumberLongType, &width); 224 if (val) CFNumberGetValue(val, kCFNumberLongType, &width);
  225 + val = CFDictionaryGetValue(properties, kCGImagePropertyOrientation);
  226 + if (val) CFNumberGetValue(val, kCFNumberNSIntegerType, &orientationValue);
223 CFRelease(properties); 227 CFRelease(properties);
  228 +
  229 + // When we draw to Core Graphics, we lose orientation information,
  230 + // which means the image below born of initWithCGIImage will be
  231 + // oriented incorrectly sometimes. (Unlike the image born of initWithData
  232 + // in connectionDidFinishLoading.) So save it here and pass it on later.
  233 + orientation = [[self class] orientationFromPropertyValue:(orientationValue == -1 ? 1 : orientationValue)];
224 } 234 }
  235 +
225 } 236 }
226 237
227 if (width + height > 0 && totalSize < self.expectedSize) { 238 if (width + height > 0 && totalSize < self.expectedSize) {
@@ -249,7 +260,7 @@ @@ -249,7 +260,7 @@
249 #endif 260 #endif
250 261
251 if (partialImageRef) { 262 if (partialImageRef) {
252 - UIImage *image = [UIImage imageWithCGImage:partialImageRef]; 263 + UIImage *image = [UIImage imageWithCGImage:partialImageRef scale:1 orientation:orientation];
253 UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image]; 264 UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image];
254 image = [UIImage decodedImageWithImage:scaledImage]; 265 image = [UIImage decodedImageWithImage:scaledImage];
255 CGImageRelease(partialImageRef); 266 CGImageRelease(partialImageRef);
@@ -269,6 +280,29 @@ @@ -269,6 +280,29 @@
269 } 280 }
270 } 281 }
271 282
  283 ++ (UIImageOrientation)orientationFromPropertyValue:(NSInteger)value {
  284 + switch (value) {
  285 + case 1:
  286 + return UIImageOrientationUp;
  287 + case 3:
  288 + return UIImageOrientationDown;
  289 + case 8:
  290 + return UIImageOrientationLeft;
  291 + case 6:
  292 + return UIImageOrientationRight;
  293 + case 2:
  294 + return UIImageOrientationUpMirrored;
  295 + case 4:
  296 + return UIImageOrientationDownMirrored;
  297 + case 5:
  298 + return UIImageOrientationLeftMirrored;
  299 + case 7:
  300 + return UIImageOrientationRightMirrored;
  301 + default:
  302 + return UIImageOrientationUp;
  303 + }
  304 +}
  305 +
272 - (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image { 306 - (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image {
273 return SDScaledImageForKey(key, image); 307 return SDScaledImageForKey(key, image);
274 } 308 }