Przeglądaj źródła

Fix an issue where images weren't appropriately rotated when saving to a PNG file and imageOrientation flag was lost.

Tom Kraina 10 lat temu
rodzic
commit
177495f436

+ 4 - 0
Kingfisher.xcodeproj/project.pbxproj

@@ -24,6 +24,7 @@
 		4BD352E71AF3681800B18A40 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4BD352E61AF3681800B18A40 /* Images.xcassets */; };
 		4BD352EA1AF3681800B18A40 /* Kingfisher-Demo WatchKit Extension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 4BD352D21AF3681800B18A40 /* Kingfisher-Demo WatchKit Extension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
 		4BD352F31AF36A0700B18A40 /* WKInterfaceImage+Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BD352F21AF36A0700B18A40 /* WKInterfaceImage+Kingfisher.swift */; };
+		9C099CCD1B727A910014D062 /* UIImage+Normalize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9C099CCC1B727A910014D062 /* UIImage+Normalize.swift */; };
 		D151E72B1AD3C48D004FD4AE /* UIImage+Decode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D151E72A1AD3C48D004FD4AE /* UIImage+Decode.swift */; };
 		D1ED2D111AD2CFA600CFC3EB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D101AD2CFA600CFC3EB /* AppDelegate.swift */; };
 		D1ED2D131AD2CFA600CFC3EB /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D121AD2CFA600CFC3EB /* ViewController.swift */; };
@@ -125,6 +126,7 @@
 		4BD352E41AF3681800B18A40 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Interface.storyboard; sourceTree = "<group>"; };
 		4BD352E61AF3681800B18A40 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
 		4BD352F21AF36A0700B18A40 /* WKInterfaceImage+Kingfisher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "WKInterfaceImage+Kingfisher.swift"; sourceTree = "<group>"; };
+		9C099CCC1B727A910014D062 /* UIImage+Normalize.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Normalize.swift"; sourceTree = "<group>"; };
 		A8D69912DD16C2942EB1F40E /* Pods-KingfisherTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KingfisherTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-KingfisherTests/Pods-KingfisherTests.release.xcconfig"; sourceTree = "<group>"; };
 		A9E621E297FEFAD35D39C34E /* libPods-KingfisherTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-KingfisherTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
 		D151E72A1AD3C48D004FD4AE /* UIImage+Decode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Decode.swift"; sourceTree = "<group>"; };
@@ -287,6 +289,7 @@
 				4B945B201ADBB20200D1545E /* UIButton+Kingfisher.swift */,
 				4BD352F21AF36A0700B18A40 /* WKInterfaceImage+Kingfisher.swift */,
 				D151E72A1AD3C48D004FD4AE /* UIImage+Decode.swift */,
+				9C099CCC1B727A910014D062 /* UIImage+Normalize.swift */,
 				D1ED2D571AD2D0F900CFC3EB /* ImageCache.swift */,
 				D1ED2D581AD2D0F900CFC3EB /* ImageDownloader.swift */,
 				D1ED2D591AD2D0F900CFC3EB /* KingfisherManager.swift */,
@@ -593,6 +596,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				9C099CCD1B727A910014D062 /* UIImage+Normalize.swift in Sources */,
 				D1ED2D5F1AD2D0F900CFC3EB /* KingfisherManager.swift in Sources */,
 				D1ED2D5B1AD2D0F900CFC3EB /* String+MD5.swift in Sources */,
 				D1ED2D5C1AD2D0F900CFC3EB /* UIImageView+Kingfisher.swift in Sources */,

+ 1 - 1
Kingfisher/ImageCache.swift

@@ -156,7 +156,7 @@ public extension ImageCache {
         
         if toDisk {
             dispatch_async(ioQueue, { () -> Void in
-                if let data = UIImagePNGRepresentation(image) {
+                if let data = UIImagePNGRepresentation(image.kf_normalizedImage()) {
                     if !self.fileManager.fileExistsAtPath(self.diskCachePath) {
                         self.fileManager.createDirectoryAtPath(self.diskCachePath, withIntermediateDirectories: true, attributes: nil, error: nil)
                     }

+ 42 - 0
Kingfisher/UIImage+Normalize.swift

@@ -0,0 +1,42 @@
+//
+//  UIImage+Normalize.swift
+//  Kingfisher
+//
+//  Created by Tom Kraina on 15/8/5.
+//
+//  Copyright (c) 2015 Wei Wang. All rights reserved.
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+
+import UIKit
+
+extension UIImage {
+    
+    func kf_normalizedImage() -> UIImage {
+        if imageOrientation == .Up { return self }
+        
+        UIGraphicsBeginImageContextWithOptions(size, false, scale)
+        drawInRect(CGRect(origin: CGPointZero, size: size))
+        let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()
+        UIGraphicsEndImageContext()
+        
+        return normalizedImage;
+    }
+}
+