|
|
@@ -527,77 +527,3 @@ extension KingfisherWrapper where Base: Image {
|
|
|
}
|
|
|
#endif
|
|
|
}
|
|
|
-
|
|
|
-extension CGImage: KingfisherCompatible {}
|
|
|
-/// High Performance Image Resizing
|
|
|
-/// @see https://nshipster.com/image-resizing/
|
|
|
-extension KingfisherWrapper where Base: CGImage {
|
|
|
- var size: CGSize {
|
|
|
- return CGSize(width: CGFloat(base.width), height: CGFloat(base.height))
|
|
|
- }
|
|
|
-
|
|
|
- /// Resizes `base` CGImage to a CGImage of new size, respecting the given content mode.
|
|
|
- ///
|
|
|
- /// - Parameters:
|
|
|
- /// - targetSize: The target size in point.
|
|
|
- /// - contentMode: Content mode of output image should be.
|
|
|
- /// - Returns: A CGImage with new size.
|
|
|
- #if os(iOS) || os(tvOS)
|
|
|
- public func resize(to size: CGSize, for contentMode: UIView.ContentMode) -> CGImage {
|
|
|
- switch contentMode {
|
|
|
- case .scaleAspectFit:
|
|
|
- return resize(to: size, for: .aspectFit)
|
|
|
- case .scaleAspectFill:
|
|
|
- return resize(to: size, for: .aspectFill)
|
|
|
- default:
|
|
|
- return resize(to: size)
|
|
|
- }
|
|
|
- }
|
|
|
- #endif
|
|
|
-
|
|
|
- // MARK: - Resize
|
|
|
- /// Resizes `base` CGImage to a CGImage with new size.
|
|
|
- ///
|
|
|
- /// - Parameter size: The target size in point.
|
|
|
- /// - Returns: A CGImage with new size.
|
|
|
- public func resize(to size: CGSize) -> CGImage {
|
|
|
- let alphaInfo = base.alphaInfo.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
|
|
|
- var hasAlpha = false
|
|
|
- if alphaInfo == CGImageAlphaInfo.premultipliedLast.rawValue
|
|
|
- || alphaInfo == CGImageAlphaInfo.premultipliedFirst.rawValue
|
|
|
- || alphaInfo == CGImageAlphaInfo.first.rawValue
|
|
|
- || alphaInfo == CGImageAlphaInfo.last.rawValue {
|
|
|
- hasAlpha = true
|
|
|
- }
|
|
|
-
|
|
|
- var bitmapInfo = CGImageByteOrderInfo.order32Little.rawValue
|
|
|
- bitmapInfo |= hasAlpha ? CGImageAlphaInfo.premultipliedFirst.rawValue : CGImageAlphaInfo.noneSkipFirst.rawValue
|
|
|
-
|
|
|
- guard let context = CGContext(data: nil,
|
|
|
- width: Int(size.width),
|
|
|
- height: Int(size.height),
|
|
|
- bitsPerComponent: base.bitsPerComponent,
|
|
|
- bytesPerRow: base.bytesPerRow,
|
|
|
- space: base.colorSpace ?? CGColorSpaceCreateDeviceRGB(),
|
|
|
- bitmapInfo: bitmapInfo) else
|
|
|
- {
|
|
|
- return base
|
|
|
- }
|
|
|
-
|
|
|
- let rect = CGRect(origin: .zero, size: size)
|
|
|
- context.interpolationQuality = .high
|
|
|
- context.draw(base, in: rect)
|
|
|
- return context.makeImage() ?? base
|
|
|
- }
|
|
|
-
|
|
|
- /// Resizes `base` CGImage to a CGImage of new size, respecting the given content mode.
|
|
|
- ///
|
|
|
- /// - Parameters:
|
|
|
- /// - targetSize: The target size in point.
|
|
|
- /// - contentMode: Content mode of output image should be.
|
|
|
- /// - Returns: A CGImage with new size.
|
|
|
- public func resize(to targetSize: CGSize, for contentMode: ContentMode) -> CGImage {
|
|
|
- let newSize = size.kf.resize(to: targetSize, for: contentMode)
|
|
|
- return resize(to: newSize)
|
|
|
- }
|
|
|
-}
|