Issue #320 talks about a crop image processor.
Do you think a center crop image processor fits into the current collection of core processors?
Sure, it could be a great improvement! Do you have time to implement it? (or I will handle it later since I'm getting quite busy for now)
I can certainly take a crack at it, with a warning that I just started using swift ;)
Do you think it makes more sense as a separate ImageProcessor, or possibly as part of options passed to the current resize processor?
I believe it would be better to be a new one :)
With crop anchor as parameter, and center as its default value.
I put together a quick centre crop processor that I needed for my app, posting it here for anyone that might need it. I have not implemented the anchor yet, if I do I'll create a pull request. :) (P.S.: The code was found on stack overflow, I just put it in an ImageProcessor)
import Foundation
import Kingfisher
/// Processor for cropping the center of an image
public struct CenterCropImageProcessor: ImageProcessor {
public let identifier: String
/// Center point to crop to.
public var centerPoint: CGFloat = 0.0
/// Initialize a `CenterCropImageProcessor`
///
/// - parameter centerPoint: The center point to crop to.
///
/// - returns: An initialized `CenterCropImageProcessor`.
public init(centerPoint: CGFloat? = nil) {
if let center = centerPoint {
self.centerPoint = center
}
self.identifier = "com.l4grange.CenterCropImageProcessor(\(centerPoint))"
}
public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
switch item {
case .image(let image):
var imageHeight = image.size.height
var imageWidth = image.size.width
if imageHeight > imageWidth {
imageHeight = imageWidth
}
else {
imageWidth = imageHeight
}
let size = CGSize(width: imageWidth, height: imageHeight)
let refWidth : CGFloat = CGFloat(image.cgImage!.width)
let refHeight : CGFloat = CGFloat(image.cgImage!.height)
let x = (refWidth - size.width) / 2
let y = (refHeight - size.height) / 2
let cropRect = CGRect(x: x, y: y, width: size.height, height: size.width)
if let imageRef = image.cgImage!.cropping(to: cropRect) {
return UIImage(cgImage: imageRef, scale: 0, orientation: image.imageOrientation)
}
return nil
case .data(_):
return (DefaultImageProcessor.default >> self).process(item: item, options: options)
}
}
}
@L4grange Good job! I'll take a look at it to see whether we could implement a built-in crop processor based on this.
Cropping image is implemented in #630 now.
how i can crop image @onevcat many thank for support, great respo!
Just create a CroppingImageProcessor and pass it as an option to setImage methods. Please have a look at our wiki for more.
Most helpful comment
Cropping image is implemented in #630 now.