Procházet zdrojové kódy

Refactor for UI drawing code

onevcat před 9 roky
rodič
revize
e364d8b0ff
2 změnil soubory, kde provedl 48 přidání a 60 odebrání
  1. 2 2
      Sources/Filter.swift
  2. 46 58
      Sources/Image.swift

+ 2 - 2
Sources/Filter.swift

@@ -21,8 +21,8 @@ public struct Filter {
     
     
     let transform: Transformer
     let transform: Transformer
     
     
-    public static var tint: (Color) -> Filter = { color in
-        
+    public static var tint: (Color) -> Filter = {
+        color in
         Filter { input in
         Filter { input in
             let colorFilter = CIFilter(name: "CIConstantColorGenerator")!
             let colorFilter = CIFilter(name: "CIConstantColorGenerator")!
             colorFilter.setValue(CIColor(color: color), forKey: kCIInputColorKey)
             colorFilter.setValue(CIColor(color: color), forKey: kCIInputColorKey)

+ 46 - 58
Sources/Image.swift

@@ -157,20 +157,13 @@ extension Image {
      */
      */
     public func kf_normalized() -> Image {
     public func kf_normalized() -> Image {
         // prevent animated image (GIF) lose it's images
         // prevent animated image (GIF) lose it's images
-        if images != nil {
-            return self
-        }
+        guard images == nil else { return self }
+        // No need to do anything if already up
+        guard imageOrientation != .up else { return self }
     
     
-        if imageOrientation == .up {
-            return self
+        return draw(cgImage: nil, to: size) {
+            draw(in: CGRect(origin: CGPoint.zero, size: size))
         }
         }
-    
-        UIGraphicsBeginImageContextWithOptions(size, false, scale)
-        draw(in: CGRect(origin: CGPoint.zero, size: size))
-        let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()
-        UIGraphicsEndImageContext()
-    
-        return normalizedImage!
     }
     }
     
     
     static func kf_animated(with images: [Image], forDuration duration: TimeInterval) -> Image? {
     static func kf_animated(with images: [Image], forDuration duration: TimeInterval) -> Image? {
@@ -360,30 +353,25 @@ extension Image {
                 return self
                 return self
             }
             }
             
             
-            return draw(cgImage: cgImage, to: size, draw: { 
+            return draw(cgImage: cgImage, to: size) {
                 let path = NSBezierPath(roundedRect: rect, xRadius: radius, yRadius: radius)
                 let path = NSBezierPath(roundedRect: rect, xRadius: radius, yRadius: radius)
                 path.windingRule = .evenOddWindingRule
                 path.windingRule = .evenOddWindingRule
                 path.addClip()
                 path.addClip()
                 draw(in: rect)
                 draw(in: rect)
-            })
+            }
         #else
         #else
-            UIGraphicsBeginImageContextWithOptions(rect.size, false, scale)
             
             
-            guard let context = UIGraphicsGetCurrentContext() else {
-                assertionFailure("[Kingfisher] Failed to create CG context for round corner image.")
-                return self
+            return draw(cgImage: nil, to: size) {
+                guard let context = UIGraphicsGetCurrentContext() else {
+                    assertionFailure("[Kingfisher] Failed to create CG context for image.")
+                    return
+                }
+                let path = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
+                context.addPath(path)
+                context.clip()
+                
+                draw(in: rect)
             }
             }
-            
-            let path = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
-            context.addPath(path)
-            context.clip()
-            
-            draw(in: rect)
-            
-            let output = UIGraphicsGetImageFromCurrentImageContext()
-            UIGraphicsEndImageContext()
-
-            return output ?? self
         #endif
         #endif
     }
     }
     
     
@@ -413,16 +401,13 @@ extension Image {
                 return self
                 return self
             }
             }
             
             
-            return draw(cgImage: cgImage, to: rect.size, draw: {
+            return draw(cgImage: cgImage, to: size, draw: {
                 draw(in: rect, from: NSRect.zero, operation: .copy, fraction: 1.0)
                 draw(in: rect, from: NSRect.zero, operation: .copy, fraction: 1.0)
             })
             })
         #else
         #else
-            UIGraphicsBeginImageContextWithOptions(size, false, scale)
-            draw(in: rect)
-            
-            let output = UIGraphicsGetImageFromCurrentImageContext()
-            UIGraphicsEndImageContext()
-            return output ?? self
+            return draw(cgImage: nil, to: size) {
+                draw(in: rect)
+            }
         #endif
         #endif
     }
     }
     
     
@@ -542,19 +527,15 @@ extension Image {
                 NSRectFillUsingOperation(rect, .sourceAtop)
                 NSRectFillUsingOperation(rect, .sourceAtop)
             })
             })
         #else
         #else
-            UIGraphicsBeginImageContextWithOptions(size, false, scale)
-
-            color.set()
-            UIRectFill(rect)
-            draw(in: rect, blendMode: .destinationIn, alpha: 1.0)
-            
-            if fraction > 0 {
-                draw(in: rect, blendMode: .sourceAtop, alpha: fraction)
+            return draw(cgImage: nil, to: size) {
+                color.set()
+                UIRectFill(rect)
+                draw(in: rect, blendMode: .destinationIn, alpha: 1.0)
+                
+                if fraction > 0 {
+                    draw(in: rect, blendMode: .sourceAtop, alpha: fraction)
+                }
             }
             }
-            
-            let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
-            UIGraphicsEndImageContext()
-            return tintedImage ?? self
         #endif
         #endif
     }
     }
     
     
@@ -586,13 +567,9 @@ extension Image {
     func kf_decoded(scale: CGFloat) -> Image? {
     func kf_decoded(scale: CGFloat) -> Image? {
         // prevent animated image (GIF) lose it's images
         // prevent animated image (GIF) lose it's images
 #if os(iOS)
 #if os(iOS)
-        if kf_imageSource != nil {
-            return self
-        }
+        if kf_imageSource != nil { return self }
 #else
 #else
-        if kf_images != nil {
-            return self
-        }
+        if kf_images != nil { return self }
 #endif
 #endif
         
         
         guard let imageRef = self.cgImage else {
         guard let imageRef = self.cgImage else {
@@ -715,14 +692,16 @@ extension CGBitmapInfo {
     }
     }
 }
 }
 
 
-#if os(macOS)
+
 extension Image {
 extension Image {
-    func draw(cgImage: CGImage, to size: CGSize, draw: ()->()) -> Image {
+    
+    func draw(cgImage: CGImage?, to size: CGSize, draw: ()->()) -> Image {
+        #if os(macOS)
         guard let rep = NSBitmapImageRep(
         guard let rep = NSBitmapImageRep(
             bitmapDataPlanes: nil,
             bitmapDataPlanes: nil,
             pixelsWide: Int(size.width),
             pixelsWide: Int(size.width),
             pixelsHigh: Int(size.height),
             pixelsHigh: Int(size.height),
-            bitsPerSample: cgImage.bitsPerComponent,
+            bitsPerSample: cgImage?.bitsPerComponent ?? 8,
             samplesPerPixel: 4,
             samplesPerPixel: 4,
             hasAlpha: true,
             hasAlpha: true,
             isPlanar: false,
             isPlanar: false,
@@ -736,6 +715,7 @@ extension Image {
         rep.size = size
         rep.size = size
         
         
         NSGraphicsContext.saveGraphicsState()
         NSGraphicsContext.saveGraphicsState()
+        
         let context = NSGraphicsContext(bitmapImageRep: rep)
         let context = NSGraphicsContext(bitmapImageRep: rep)
         NSGraphicsContext.setCurrent(context)
         NSGraphicsContext.setCurrent(context)
         draw()
         draw()
@@ -744,9 +724,17 @@ extension Image {
         let outputImage = Image(size: size)
         let outputImage = Image(size: size)
         outputImage.addRepresentation(rep)
         outputImage.addRepresentation(rep)
         return outputImage
         return outputImage
+        #else
+            
+        UIGraphicsBeginImageContextWithOptions(size, false, scale)
+        defer { UIGraphicsEndImageContext() }
+        draw()
+        return UIGraphicsGetImageFromCurrentImageContext() ?? self
+        
+        #endif
     }
     }
 }
 }
-#endif
+
 
 
 extension CGContext {
 extension CGContext {
     static func createARGBContext(from imageRef: CGImage) -> CGContext? {
     static func createARGBContext(from imageRef: CGImage) -> CGContext? {