Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appreciate UIImage.scale when decompressing images #42

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Nuke/Source/Core/ImageLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension ImageLoaderDelegate {
#if os(OSX)
return nil
#else
return ImageDecompressor(targetSize: request.targetSize, contentMode: request.contentMode)
return ImageDecompressor(targetSize: request.targetSize, targetScale: request.targetScale, contentMode: request.contentMode)
#endif
}
}
Expand Down
28 changes: 12 additions & 16 deletions Nuke/Source/Core/ImageProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ public func ==(lhs: ImageProcessorComposition, rhs: ImageProcessorComposition) -

public class ImageDecompressor: ImageProcessing, Equatable {
public let targetSize: CGSize
public let targetScale: CGFloat
public let contentMode: ImageContentMode

public init(targetSize: CGSize = ImageMaximumSize, contentMode: ImageContentMode = .AspectFill) {
public init(targetSize: CGSize = ImageMaximumSize, targetScale: CGFloat = 1, contentMode: ImageContentMode = .AspectFill) {
self.targetSize = targetSize
self.targetScale = targetScale
self.contentMode = contentMode
}

public func processImage(image: Image) -> Image? {
return decompressImage(image, targetSize: self.targetSize, contentMode: self.contentMode)
return decompressImage(image, targetSize: self.targetSize, targetScale: self.targetScale, contentMode: self.contentMode)
}
}

Expand All @@ -88,20 +90,14 @@ public func ==(lhs: ImageProcessorComposition, rhs: ImageProcessorComposition) -

// MARK: - Misc

private func decompressImage(image: UIImage, targetSize: CGSize, contentMode: ImageContentMode) -> UIImage {
let bitmapSize = CGSize(width: CGImageGetWidth(image.CGImage), height: CGImageGetHeight(image.CGImage))
let scaleWidth = targetSize.width / bitmapSize.width
let scaleHeight = targetSize.height / bitmapSize.height
let scale = contentMode == .AspectFill ? max(scaleWidth, scaleHeight) : min(scaleWidth, scaleHeight)
return decompressImage(image, scale: Double(scale))
}

private func decompressImage(image: UIImage, scale: Double) -> UIImage {
private func decompressImage(image: UIImage, targetSize: CGSize, targetScale: CGFloat, contentMode: ImageContentMode) -> UIImage {
let imageRef = image.CGImage
var imageSize = CGSize(width: CGImageGetWidth(imageRef), height: CGImageGetHeight(imageRef))
if scale < 1.0 {
imageSize = CGSize(width: Double(imageSize.width) * scale, height: Double(imageSize.height) * scale)
}
let bitmapSize = CGSize(width: CGImageGetWidth(imageRef), height: CGImageGetHeight(imageRef))
let scaleWidth = targetScale * targetSize.width / bitmapSize.width
let scaleHeight = targetScale * targetSize.height / bitmapSize.height
let minification = min(1, contentMode == .AspectFill ? max(scaleWidth, scaleHeight) : min(scaleWidth, scaleHeight))
let imageSize = CGSize(width: round(CGFloat(CGImageGetWidth(imageRef)) * minification),
height: round(CGFloat(CGImageGetHeight(imageRef)) * minification))
// See Quartz 2D Programming Guide and https://github.com/kean/Nuke/issues/35 for more info
guard let contextRef = CGBitmapContextCreate(nil, Int(imageSize.width), Int(imageSize.height), 8, 0, CGColorSpaceCreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast.rawValue) else {
return image
Expand All @@ -110,7 +106,7 @@ public func ==(lhs: ImageProcessorComposition, rhs: ImageProcessorComposition) -
guard let decompressedImageRef = CGBitmapContextCreateImage(contextRef) else {
return image
}
return UIImage(CGImage: decompressedImageRef, scale: image.scale, orientation: image.imageOrientation)
return UIImage(CGImage: decompressedImageRef, scale: targetScale, orientation: image.imageOrientation)
}

#endif
4 changes: 4 additions & 0 deletions Nuke/Source/Core/ImageRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public struct ImageRequest {
*/
public var targetSize: CGSize = ImageMaximumSize

/** Target scale in pixels per screen point. Default value is 1.
*/
public var targetScale: CGFloat = 1

/** Content mode. Default value is .AspectFill.
*/
public var contentMode: ImageContentMode = .AspectFill
Expand Down