diff --git a/Demo/Kingfisher-Demo/Images.xcassets/AppIcon.appiconset/Contents.json b/Demo/Kingfisher-Demo/Images.xcassets/AppIcon.appiconset/Contents.json index 36d2c80d8..1d060ed28 100644 --- a/Demo/Kingfisher-Demo/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/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" : { diff --git a/Sources/Image.swift b/Sources/Image.swift index e45d98637..22c3e1f93 100755 --- a/Sources/Image.swift +++ b/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(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, diff --git a/Sources/ImageCache.swift b/Sources/ImageCache.swift index 4d0bf76e8..fc0f210d7 100755 --- a/Sources/ImageCache.swift +++ b/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, diff --git a/Sources/ImageDownloader.swift b/Sources/ImageDownloader.swift index f2bf35fe2..3d7c18ab3 100755 --- a/Sources/ImageDownloader.swift +++ b/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) } diff --git a/Tests/KingfisherTests/ImageExtensionTests.swift b/Tests/KingfisherTests/ImageExtensionTests.swift index 8fa3845b7..19b9e934c 100755 --- a/Tests/KingfisherTests/ImageExtensionTests.swift +++ b/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(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 + + } }