Permalink
Browse files

Merge pull request #633 from hlian/progressive-orientation

SDWebImageDownloaderOperation: pass orientation to initWithCGImage during progressive rendering
  • Loading branch information...
2 parents 10ff189 + c13ec87 commit 895249b3125ac42bcffb1343588f70238cdb3cf6 @rs committed Feb 13, 2014
Showing with 35 additions and 1 deletion.
  1. +35 −1 SDWebImage/SDWebImageDownloaderOperation.m
View
36 SDWebImage/SDWebImageDownloaderOperation.m
@@ -32,6 +32,7 @@ @interface SDWebImageDownloaderOperation ()
@implementation SDWebImageDownloaderOperation {
size_t width, height;
+ UIImageOrientation orientation;
BOOL responseFromCached;
}
@@ -216,12 +217,22 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (width + height == 0) {
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
if (properties) {
+ NSInteger orientationValue = -1;
CFTypeRef val = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
if (val) CFNumberGetValue(val, kCFNumberLongType, &height);
val = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
if (val) CFNumberGetValue(val, kCFNumberLongType, &width);
+ val = CFDictionaryGetValue(properties, kCGImagePropertyOrientation);
+ if (val) CFNumberGetValue(val, kCFNumberNSIntegerType, &orientationValue);
CFRelease(properties);
+
+ // When we draw to Core Graphics, we lose orientation information,
+ // which means the image below born of initWithCGIImage will be
+ // oriented incorrectly sometimes. (Unlike the image born of initWithData
+ // in connectionDidFinishLoading.) So save it here and pass it on later.
+ orientation = [[self class] orientationFromPropertyValue:(orientationValue == -1 ? 1 : orientationValue)];
}
+
}
if (width + height > 0 && totalSize < self.expectedSize) {
@@ -249,7 +260,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
#endif
if (partialImageRef) {
- UIImage *image = [UIImage imageWithCGImage:partialImageRef];
+ UIImage *image = [UIImage imageWithCGImage:partialImageRef scale:1 orientation:orientation];
UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image];
image = [UIImage decodedImageWithImage:scaledImage];
CGImageRelease(partialImageRef);
@@ -269,6 +280,29 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
}
++ (UIImageOrientation)orientationFromPropertyValue:(NSInteger)value {
+ switch (value) {
+ case 1:
+ return UIImageOrientationUp;
+ case 3:
+ return UIImageOrientationDown;
+ case 8:
+ return UIImageOrientationLeft;
+ case 6:
+ return UIImageOrientationRight;
+ case 2:
+ return UIImageOrientationUpMirrored;
+ case 4:
+ return UIImageOrientationDownMirrored;
+ case 5:
+ return UIImageOrientationLeftMirrored;
+ case 7:
+ return UIImageOrientationRightMirrored;
+ default:
+ return UIImageOrientationUp;
+ }
+}
+
- (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image {
return SDScaledImageForKey(key, image);
}

0 comments on commit 895249b

Please sign in to comment.