Explorar o código

Also use downsampling when provided with image

onevcat %!s(int64=7) %!d(string=hai) anos
pai
achega
c653d494c2

+ 1 - 11
Sources/Cache/CacheSerializer.swift

@@ -97,17 +97,7 @@ public struct DefaultCacheSerializer: CacheSerializer {
     ///
     /// If `original` is `nil`, the input `image` will be encoded as PNG data.
     public func data(with image: Image, original: Data?) -> Data? {
-        let imageFormat = original?.kf.imageFormat ?? .unknown
-
-        let data: Data?
-        switch imageFormat {
-        case .PNG: data = image.kf.pngRepresentation()
-        case .JPEG: data = image.kf.jpegRepresentation(compressionQuality: 1.0)
-        case .GIF: data = image.kf.gifRepresentation()
-        case .unknown: data = original ?? image.kf.normalized.kf.pngRepresentation()
-        }
-
-        return data
+        return image.kf.data(format: original?.kf.imageFormat ?? .unknown)
     }
     
     /// Gets an image deserialized from provided data.

+ 17 - 0
Sources/Image/Image.swift

@@ -179,6 +179,23 @@ extension KingfisherWrapper where Base: Image {
     public func gifRepresentation() -> Data? {
         return animatedImageData
     }
+
+    /// Returns a data representation for `base` image, with the `format` as the format indicator.
+    ///
+    /// - Parameter format: The format in which the output data should be. If `unknown`, the `base` image will be
+    ///                     converted in the PNG representation.
+    /// - Returns: The output data representing.
+    public func data(format: ImageFormat) -> Data? {
+        let data: Data?
+        switch format {
+        case .PNG: data = pngRepresentation()
+        case .JPEG: data = jpegRepresentation(compressionQuality: 1.0)
+        case .GIF: data = gifRepresentation()
+        case .unknown: data = normalized.kf.pngRepresentation()
+        }
+
+        return data
+    }
 }
 
 // MARK: - Creating Images

+ 4 - 7
Sources/Image/ImageProcessor.swift

@@ -761,11 +761,6 @@ public struct CroppingImageProcessor: ImageProcessor {
 /// does not render the images to resize. Instead, it downsample the input data directly to an
 /// image. It is a more efficient than `ResizingImageProcessor`.
 ///
-/// - Note:
-/// Downsampling only happens when this processor used as the first processor in a processing
-/// pipeline, when the input `ImageProcessItem` is an `.data` value. If appending to any other
-/// processors, it falls back to use the normal rendering resizing behavior.
-///
 /// Only CG-based images are supported. Animated images (like GIF) is not supported.
 public struct DownsamplingImageProcessor: ImageProcessor {
     
@@ -797,8 +792,10 @@ public struct DownsamplingImageProcessor: ImageProcessor {
     public func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> Image? {
         switch item {
         case .image(let image):
-            return image.kf.scaled(to: options.scaleFactor)
-                        .kf.resize(to: size, for: .none)
+            guard let data = image.kf.data(format: .unknown) else {
+                return nil
+            }
+            return KingfisherWrapper.downsampledImage(data: data, to: size, scale: options.scaleFactor)
         case .data(let data):
             return KingfisherWrapper.downsampledImage(data: data, to: size, scale: options.scaleFactor)
         }

+ 1 - 1
Tests/KingfisherTests/ImageModifierTests.swift

@@ -1,5 +1,5 @@
 //
-//  ImageProcessorTests.swift
+//  ImageModifierTests.swift
 //  Kingfisher
 //
 //  Created by Ethan Gill on 2017/11/29.