Skip to content

Commit

Permalink
Merge pull request onevcat#760 from onevcat/fix/bg-decode-scale
Browse files Browse the repository at this point in the history
Take scale to context for decoding
  • Loading branch information
onevcat authored Sep 2, 2017
2 parents bb1aa00 + 2f4968b commit db409d6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -30,6 +40,16 @@
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
Expand Down Expand Up @@ -59,6 +79,11 @@
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
Expand Down
13 changes: 7 additions & 6 deletions Sources/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Sources/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Sources/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
25 changes: 25 additions & 0 deletions Tests/KingfisherTests/ImageExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
}

0 comments on commit db409d6

Please sign in to comment.