Просмотр исходного кода

Tests for normalized image orientation

onevcat 6 лет назад
Родитель
Сommit
6f2474373e

+ 2 - 2
Sources/Image/Image.swift

@@ -121,9 +121,9 @@ extension KingfisherWrapper where Base: Image {
     /// This method will try to redraw an image with orientation and scale considered.
     public var normalized: Image {
         // prevent animated image (GIF) lose it's images
-        guard images == nil else { return base }
+        guard images == nil else { return base.copy() as! Image }
         // No need to do anything if already up
-        guard base.imageOrientation != .up else { return base }
+        guard base.imageOrientation != .up else { return base.copy() as! Image }
 
         return draw(to: size, inverting: true, refImage: Image()) {
             fixOrientation(in: $0)

+ 10 - 1
Tests/KingfisherTests/ImageExtensionTests.swift

@@ -287,7 +287,16 @@ class ImageExtensionTests: XCTestCase {
         // No need to normalize up orientation image.
         let normalImage = testImage
         XCTAssertEqual(normalImage.imageOrientation, .up)
-        XCTAssertEqual(normalImage, testImage)
+        XCTAssertEqual(normalImage.kf.normalized, testImage)
+
+        let colorImage = UIImage.from(color: .red, size: CGSize(width: 100, height: 200))
+        let rotatedImage = UIImage(cgImage: colorImage.cgImage!, scale: colorImage.scale, orientation: .right)
+
+        XCTAssertEqual(rotatedImage.imageOrientation, .right)
+
+        let rotatedNormalizedImage = rotatedImage.kf.normalized
+        XCTAssertEqual(rotatedNormalizedImage.imageOrientation, .up)
+        XCTAssertEqual(rotatedNormalizedImage.size, CGSize(width: 200, height: 100))
         #endif
     }
     

+ 16 - 0
Tests/KingfisherTests/KingfisherTestHelper.swift

@@ -196,6 +196,22 @@ extension Image {
     }
 }
 
+#if os(iOS) || os(tvOS)
+import UIKit
+extension Image {
+    static func from(color: Color, size: CGSize) -> Image {
+        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
+        UIGraphicsBeginImageContext(rect.size)
+        let context = UIGraphicsGetCurrentContext()
+        context!.setFillColor(color.cgColor)
+        context!.fill(rect)
+        let img = UIGraphicsGetImageFromCurrentImageContext()
+        UIGraphicsEndImageContext()
+        return img!
+    }
+}
+#endif
+
 extension Data {
     init(fileName: String) {
         let comp = fileName.components(separatedBy: ".")