8
8
9
9
#import " SDImageWebPCoder.h"
10
10
#import " SDWebImageWebPCoderDefine.h"
11
+ #import " SDInternalMacros.h"
11
12
#import < Accelerate/Accelerate.h>
12
13
#import < os/lock.h>
13
14
#import < libkern/OSAtomic.h>
@@ -784,7 +785,14 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
784
785
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue ];
785
786
if (encodeFirstFrame || frames.count <= 1 ) {
786
787
// for static single webp image
788
+ // Keep EXIF orientation
789
+ #if SD_UIKIT || SD_WATCH
790
+ CGImagePropertyOrientation orientation = [SDImageCoderHelper exifOrientationFromImageOrientation: image.imageOrientation];
791
+ #else
792
+ CGImagePropertyOrientation orientation = kCGImagePropertyOrientationUp ;
793
+ #endif
787
794
data = [self sd_encodedWebpDataWithImage: imageRef
795
+ orientation: orientation
788
796
quality: compressionQuality
789
797
maxPixelSize: maxPixelSize
790
798
maxFileSize: maxFileSize
@@ -797,7 +805,15 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
797
805
}
798
806
for (size_t i = 0 ; i < frames.count ; i++) {
799
807
SDImageFrame *currentFrame = frames[i];
800
- NSData *webpData = [self sd_encodedWebpDataWithImage: currentFrame.image.CGImage
808
+ UIImage *currentImage = currentFrame.image ;
809
+ // Keep EXIF orientation
810
+ #if SD_UIKIT || SD_WATCH
811
+ CGImagePropertyOrientation orientation = [SDImageCoderHelper exifOrientationFromImageOrientation: currentImage.imageOrientation];
812
+ #else
813
+ CGImagePropertyOrientation orientation = kCGImagePropertyOrientationUp ;
814
+ #endif
815
+ NSData *webpData = [self sd_encodedWebpDataWithImage: currentImage.CGImage
816
+ orientation: orientation
801
817
quality: compressionQuality
802
818
maxPixelSize: maxPixelSize
803
819
maxFileSize: maxFileSize
@@ -838,6 +854,7 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
838
854
}
839
855
840
856
- (nullable NSData *)sd_encodedWebpDataWithImage : (nullable CGImageRef )imageRef
857
+ orientation : (CGImagePropertyOrientation)orientation
841
858
quality : (double )quality
842
859
maxPixelSize : (CGSize )maxPixelSize
843
860
maxFileSize : (NSUInteger )maxFileSize
@@ -847,6 +864,20 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
847
864
if (!imageRef) {
848
865
return nil ;
849
866
}
867
+ // Seems libwebp has no convenient EXIF orientation API ?
868
+ // Use transform to apply ourselves. Need to release before return
869
+ // TODO: Use `WebPMuxSetChunk` API to write/read EXIF data, see: https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
870
+ __block CGImageRef rotatedCGImage = NULL ;
871
+ @onExit {
872
+ if (rotatedCGImage) {
873
+ CGImageRelease (rotatedCGImage);
874
+ }
875
+ };
876
+ if (orientation != kCGImagePropertyOrientationUp ) {
877
+ rotatedCGImage = [SDImageCoderHelper CGImageCreateDecoded: imageRef orientation: orientation];
878
+ NSCParameterAssert (rotatedCGImage);
879
+ imageRef = rotatedCGImage;
880
+ }
850
881
851
882
size_t width = CGImageGetWidth (imageRef);
852
883
size_t height = CGImageGetHeight (imageRef);
0 commit comments