TimOliver / TOCropViewController

A view controller for iOS that allows users to crop portions of UIImage objects
http://www.timoliver.com.au/2015/06/21/tocropviewcontroller-an-open-source-image-cropper-for-ios/
MIT License
4.73k stars 951 forks source link

Feature request - cropping to exact dimensions #478

Open sabiland opened 3 years ago

sabiland commented 3 years ago

It would be really cool if there was an option-logic for cropping to exact dimensions.

Anyways, great library !

adincebic commented 3 years ago

It would be really cool if there was an option-logic for cropping to exact dimensions.

Anyways, great library !

Feel free to do that yourself and make a pull request.

sabiland commented 3 years ago

Well sorry mate, besides my regular programming job I work also on my iOS app a lot, I do not have time to do it alone.

It is really a nice library, I integrated it into my app in 5 minutes :).

TimOliver commented 3 years ago

Thanks for the feedback @sabiland! I'm glad you're enjoying the library.

Hmm, how exactly do you mean cropping to exact dimensions exactly? How would you see this working?

Haha, I also share the same sentiments about spending time between work and my iOS side projects. 🤣

sabiland commented 3 years ago

Well I was thinking just to choose exact width/height (in pixels) for cropping and then you would just move fixed rectangle around image. Though I am not sure if this feature would be helpful for most of the users.

jbafford commented 3 years ago

I needed a subset of this functionality: specifying a minimum pixel dimension. That can be obtained by configuring the TOCropViewController/TOCropView as follows:

let image: UIImage = ...
let cropController: TOCropViewController = ...
let minimumSize: CGSize = ...

cropController.aspectRatioLockEnabled = true
cropController.customAspectRatio = minimumSize
cropController.allowedAspectRatios = [
    NSNumber(value: TOCropViewControllerAspectRatioPreset.presetCustom.rawValue)
]

let wRatio = image.size.width / minimumSize.width
let hRatio = image.size.height / minimumSize.height

cropController.cropView.maximumZoomScale = min(wRatio, hRatio)

Though I wouldn't mind a more straightforward way of specifying that supported directly by TOCropView, that code works for me.

If you really needed an exact height and width (which I don't, so I'm not going to write the code to do it), at a glance it seems like the most straightforward way would be to add a minimumZoomScale property to the TOCropView, and connect it to the underlying scrollView in -[TOCropView layoutInitialImage] and -[TOCropView moveCroppedContentToCenterAnimated:]. Then, set both the minimum and maximum zoom scales to the same value.