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

Center crop ImageProcessor #465

Closed
newmanw opened this issue Oct 4, 2016 · 9 comments
Closed

Center crop ImageProcessor #465

newmanw opened this issue Oct 4, 2016 · 9 comments

Comments

@newmanw
Copy link

newmanw commented Oct 4, 2016

Issue #320 talks about a crop image processor.

Do you think a center crop image processor fits into the current collection of core processors?

@onevcat
Copy link
Owner

onevcat commented Oct 5, 2016

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)

@newmanw
Copy link
Author

newmanw commented Oct 5, 2016

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?

@onevcat
Copy link
Owner

onevcat commented Oct 5, 2016

I believe it would be better to be a new one :)

@onevcat
Copy link
Owner

onevcat commented Oct 5, 2016

With crop anchor as parameter, and center as its default value.

@lpbas
Copy link

lpbas commented Mar 5, 2017

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)
			}
		}
}

@onevcat
Copy link
Owner

onevcat commented Mar 13, 2017

@L4grange Good job! I'll take a look at it to see whether we could implement a built-in crop processor based on this.

@onevcat
Copy link
Owner

onevcat commented Mar 26, 2017

Cropping image is implemented in #630 now.

@onevcat onevcat closed this as completed Mar 26, 2017
@mhtranbn
Copy link

how i can crop image @onevcat many thank for support, great respo!

@onevcat
Copy link
Owner

onevcat commented Mar 27, 2017

Just create a CroppingImageProcessor and pass it as an option to setImage methods. Please have a look at our wiki for more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants