diff --git a/SDWebImage/Core/SDImageIOAnimatedCoder.m b/SDWebImage/Core/SDImageIOAnimatedCoder.m index 1b71b54e..10856ddc 100644 --- a/SDWebImage/Core/SDImageIOAnimatedCoder.m +++ b/SDWebImage/Core/SDImageIOAnimatedCoder.m @@ -32,6 +32,8 @@ NSUInteger _frameCount; NSArray *_frames; BOOL _finished; + BOOL _preserveAspectRatio; + CGSize _thumbnailSize; } - (void)dealloc @@ -164,6 +166,10 @@ thumbnailOptions[(__bridge NSString *)kCGImageSourceThumbnailMaxPixelSize] = preserveAspectRatio ? @(MAX(thumbnailSize.width, thumbnailSize.height)) : @(MIN(thumbnailSize.width, thumbnailSize.height)); thumbnailOptions[(__bridge NSString *)kCGImageSourceCreateThumbnailFromImageIfAbsent] = @(YES); imageRef = CGImageSourceCreateThumbnailAtIndex(source, index, (__bridge CFDictionaryRef)thumbnailOptions); + if (preserveAspectRatio) { + // kCGImageSourceCreateThumbnailWithTransform will apply EXIF transform as well, we should not apply twice + exifOrientation = kCGImagePropertyOrientationUp; + } } if (!imageRef) { return nil; @@ -276,6 +282,22 @@ scale = MAX([scaleFactor doubleValue], 1); } _scale = scale; + CGSize thumbnailSize = CGSizeZero; + NSValue *thumbnailSizeValue = options[SDImageCoderDecodeThumbnailPixelSize]; + if (thumbnailSizeValue != nil) { + #if SD_MAC + thumbnailSize = thumbnailSizeValue.sizeValue; + #else + thumbnailSize = thumbnailSizeValue.CGSizeValue; + #endif + } + _thumbnailSize = thumbnailSize; + BOOL preserveAspectRatio = NO; + NSNumber *preserveAspectRatioValue = options[SDImageCoderDecodePreserveAspectRatio]; + if (preserveAspectRatioValue != nil) { + preserveAspectRatio = preserveAspectRatioValue.boolValue; + } + _preserveAspectRatio = preserveAspectRatio; #if SD_UIKIT [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; #endif @@ -316,20 +338,8 @@ if (_width + _height > 0) { // Create the image - CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(_imageSource, 0, NULL); - - if (partialImageRef) { - CGFloat scale = _scale; - NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; - if (scaleFactor != nil) { - scale = MAX([scaleFactor doubleValue], 1); - } -#if SD_UIKIT || SD_WATCH - image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:UIImageOrientationUp]; -#else - image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:kCGImagePropertyOrientationUp]; -#endif - CGImageRelease(partialImageRef); + image = [SDImageIOAnimatedCoder createFrameAtIndex:0 source:_imageSource scale:_scale preserveAspectRatio:_preserveAspectRatio thumbnailSize:_thumbnailSize]; + if (image) { image.sd_imageFormat = self.class.imageFormat; } } diff --git a/SDWebImage/Core/SDImageIOCoder.m b/SDWebImage/Core/SDImageIOCoder.m index c17c9f16..6e80d1a4 100644 --- a/SDWebImage/Core/SDImageIOCoder.m +++ b/SDWebImage/Core/SDImageIOCoder.m @@ -20,6 +20,8 @@ CGImageSourceRef _imageSource; CGFloat _scale; BOOL _finished; + BOOL _preserveAspectRatio; + CGSize _thumbnailSize; } - (void)dealloc { @@ -122,6 +124,22 @@ scale = MAX([scaleFactor doubleValue], 1); } _scale = scale; + CGSize thumbnailSize = CGSizeZero; + NSValue *thumbnailSizeValue = options[SDImageCoderDecodeThumbnailPixelSize]; + if (thumbnailSizeValue != nil) { + #if SD_MAC + thumbnailSize = thumbnailSizeValue.sizeValue; + #else + thumbnailSize = thumbnailSizeValue.CGSizeValue; + #endif + } + _thumbnailSize = thumbnailSize; + BOOL preserveAspectRatio = NO; + NSNumber *preserveAspectRatioValue = options[SDImageCoderDecodePreserveAspectRatio]; + if (preserveAspectRatioValue != nil) { + preserveAspectRatio = preserveAspectRatioValue.boolValue; + } + _preserveAspectRatio = preserveAspectRatio; #if SD_UIKIT [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; #endif @@ -167,21 +185,8 @@ if (_width + _height > 0) { // Create the image - CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(_imageSource, 0, NULL); - - if (partialImageRef) { - CGFloat scale = _scale; - NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; - if (scaleFactor != nil) { - scale = MAX([scaleFactor doubleValue], 1); - } -#if SD_UIKIT || SD_WATCH - UIImageOrientation imageOrientation = [SDImageCoderHelper imageOrientationFromEXIFOrientation:_orientation]; - image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:imageOrientation]; -#else - image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:_orientation]; -#endif - CGImageRelease(partialImageRef); + image = [SDImageIOAnimatedCoder createFrameAtIndex:0 source:_imageSource scale:_scale preserveAspectRatio:_preserveAspectRatio thumbnailSize:_thumbnailSize]; + if (image) { CFStringRef uttype = CGImageSourceGetType(_imageSource); image.sd_imageFormat = [NSData sd_imageFormatFromUTType:uttype]; }