Skip to content

Commit

Permalink
Merge pull request #1074 from onevcat/fix/zero-size-downsampling
Browse files Browse the repository at this point in the history
Return nil for failing on source creating
  • Loading branch information
onevcat authored Dec 17, 2018
2 parents c375b89 + 4e52a3a commit af2d948
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Sources/Image/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ extension KingfisherWrapper where Base: Image {
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels] as CFDictionary
let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions)!
guard let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions) else {
return nil
}
return KingfisherWrapper.image(cgImage: downsampledImage, scale: scale, refImage: nil)
}
}
17 changes: 12 additions & 5 deletions Tests/KingfisherTests/ImageExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,6 @@ class ImageExtensionTests: XCTestCase {
let image = KingfisherWrapper<Image>.downsampledImage(data: testImageData, to: size, scale: 1)
XCTAssertEqual(image?.size, size)
XCTAssertEqual(image?.kf.scale, 1.0)

let largerSize = CGSize(width: 100, height: 100)
let largerImage = KingfisherWrapper<Image>.downsampledImage(data: testImageData, to: largerSize, scale: 1)
// You can not "downsample" an image to a larger size.
XCTAssertEqual(largerImage?.size, CGSize(width: 64, height: 64))
}

func testDownsamplingWithScale() {
Expand All @@ -314,4 +309,16 @@ class ImageExtensionTests: XCTestCase {
XCTAssertEqual(image?.kf.scale, 2.0)
#endif
}

func testDownsamplingWithEdgeCaseSize() {

// Zero size would fail downsampling.
let nilImage = KingfisherWrapper<Image>.downsampledImage(data: testImageData, to: .zero, scale: 1)
XCTAssertNil(nilImage)

let largerSize = CGSize(width: 100, height: 100)
let largerImage = KingfisherWrapper<Image>.downsampledImage(data: testImageData, to: largerSize, scale: 1)
// You can not "downsample" an image to a larger size.
XCTAssertEqual(largerImage?.size, CGSize(width: 64, height: 64))
}
}

0 comments on commit af2d948

Please sign in to comment.