Browse Source

Take scale to context for decoding

onevcat 8 years ago
parent
commit
de3914fdf1

+ 25 - 0
Demo/Kingfisher-Demo/Images.xcassets/AppIcon.appiconset/Contents.json

@@ -1,5 +1,15 @@
 {
   "images" : [
+    {
+      "idiom" : "iphone",
+      "size" : "20x20",
+      "scale" : "2x"
+    },
+    {
+      "idiom" : "iphone",
+      "size" : "20x20",
+      "scale" : "3x"
+    },
     {
       "idiom" : "iphone",
       "size" : "29x29",
@@ -30,6 +40,16 @@
       "size" : "60x60",
       "scale" : "3x"
     },
+    {
+      "idiom" : "ipad",
+      "size" : "20x20",
+      "scale" : "1x"
+    },
+    {
+      "idiom" : "ipad",
+      "size" : "20x20",
+      "scale" : "2x"
+    },
     {
       "idiom" : "ipad",
       "size" : "29x29",
@@ -59,6 +79,11 @@
       "idiom" : "ipad",
       "size" : "76x76",
       "scale" : "2x"
+    },
+    {
+      "idiom" : "ipad",
+      "size" : "83.5x83.5",
+      "scale" : "2x"
     }
   ],
   "info" : {

+ 7 - 6
Sources/Image.swift

@@ -506,7 +506,7 @@ extension Kingfisher where Base: Image {
                 return vImage_Buffer(data: data, height: height, width: width, rowBytes: rowBytes)
             }
 
-            guard let context = beginContext(size: size) else {
+            guard let context = beginContext(size: size, scale: scale) else {
                 assertionFailure("[Kingfisher] Failed to create CG context for blurring image.")
                 return base
             }
@@ -516,7 +516,7 @@ extension Kingfisher where Base: Image {
             
             var inBuffer = createEffectBuffer(context)
             
-            guard let outContext = beginContext(size: size) else {
+            guard let outContext = beginContext(size: size, scale: scale) else {
                 assertionFailure("[Kingfisher] Failed to create CG context for blurring image.")
                 return base
             }
@@ -615,7 +615,7 @@ extension Kingfisher where Base: Image {
 
 // MARK: - Decode
 extension Kingfisher where Base: Image {
-    var decoded: Image? {
+    var decoded: Image {
         return decoded(scale: scale)
     }
     
@@ -632,14 +632,15 @@ extension Kingfisher where Base: Image {
             return base
         }
         
-        guard let context = beginContext(size: CGSize(width: imageRef.width, height: imageRef.height)) else {
+        // Draw CGImage in a plain context with scale of 1.0.
+        guard let context = beginContext(size: CGSize(width: imageRef.width, height: imageRef.height), scale: 1.0) else {
             assertionFailure("[Kingfisher] Decoding fails to create a valid context.")
             return base
         }
         
         defer { endContext() }
         
-        let rect = CGRect(x: 0, y: 0, width: imageRef.width, height: imageRef.height)
+        let rect = CGRect(x: 0, y: 0, width: CGFloat(imageRef.width), height: CGFloat(imageRef.height))
         context.draw(imageRef, in: rect)
         let decompressedImageRef = context.makeImage()
         return Kingfisher<Image>.image(cgImage: decompressedImageRef!, scale: scale, refImage: base)
@@ -767,7 +768,7 @@ extension Comparable {
 
 extension Kingfisher where Base: Image {
     
-    func beginContext(size: CGSize) -> CGContext? {
+    func beginContext(size: CGSize, scale: CGFloat) -> CGContext? {
         #if os(macOS)
             guard let rep = NSBitmapImageRep(
                 bitmapDataPlanes: nil,

+ 1 - 1
Sources/ImageCache.swift

@@ -295,7 +295,7 @@ open class ImageCache {
                 if let image = sSelf.retrieveImageInDiskCache(forKey: key, options: options) {
                     if options.backgroundDecode {
                         sSelf.processQueue.async {
-                            let result = image.kf.decoded(scale: options.scaleFactor)
+                            let result = image.kf.decoded
                             
                             sSelf.store(result,
                                         forKey: key,

+ 1 - 1
Sources/ImageDownloader.swift

@@ -562,7 +562,7 @@ class ImageDownloaderSessionHandler: NSObject, URLSessionDataDelegate, Authentic
                     downloader.delegate?.imageDownloader(downloader, didDownload: image, for: url, with: task.response)
                     
                     if options.backgroundDecode {
-                        let decodedImage = image.kf.decoded(scale: options.scaleFactor)
+                        let decodedImage = image.kf.decoded
                         callbackQueue.safeAsync { completionHandler?(decodedImage, nil, url, data) }
                     } else {
                         callbackQueue.safeAsync { completionHandler?(image, nil, url, data) }

+ 25 - 0
Tests/KingfisherTests/ImageExtensionTests.swift

@@ -184,4 +184,29 @@ class ImageExtensionTests: XCTestCase {
         XCTAssertEqual(size.kf.constrainedRect(for: outY, anchor: invalidAnchor), CGRect(x:0, y: 0, width: 20, height: 100))
         XCTAssertEqual(size.kf.constrainedRect(for: outSize, anchor: invalidAnchor), CGRect(x: 0, y: 0, width: 100, height: 100))
     }
+    
+    func testDecodeScale() {
+        #if os(iOS) || os(tvOS)
+        let image = testImage
+        XCTAssertEqual(image.size, CGSize(width: 64, height: 64))
+        XCTAssertEqual(image.scale, 1.0)
+
+        let image_2x = Kingfisher<Image>.image(cgImage: image.cgImage!, scale: 2.0, refImage: image)
+        XCTAssertEqual(image_2x.size, CGSize(width: 32, height: 32))
+        XCTAssertEqual(image_2x.scale, 2.0)
+        
+        let decoded = image.kf.decoded
+        XCTAssertEqual(decoded.size, CGSize(width: 64, height: 64))
+        XCTAssertEqual(decoded.scale, 1.0)
+        
+        let decodedDifferentScale = image.kf.decoded(scale: 2.0)
+        XCTAssertEqual(decodedDifferentScale.size, CGSize(width: 32, height: 32))
+        XCTAssertEqual(decodedDifferentScale.scale, 2.0)
+        
+        let decoded_2x = image_2x.kf.decoded
+        XCTAssertEqual(decoded_2x.size, CGSize(width: 32, height: 32))
+        XCTAssertEqual(decoded_2x.scale, 2.0)
+        #endif
+        
+    }
 }