-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Center crop ImageProcessor #465
Comments
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 |
Issue #320 talks about a crop image processor.
Do you think a center crop image processor fits into the current collection of core processors?
The text was updated successfully, but these errors were encountered: