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

How can I specify aspect ratio? #7

Closed retsohuang closed 8 years ago

retsohuang commented 9 years ago

I want user crop image with specify size (or aspect ratio e.g 4x3) and without any toolbar buttons, is that possible?

TimOliver commented 9 years ago

Hi Retso!

Oh... no that's no presently possible in the current implementation, sorry.

It would be relatively simple to implement though. You'd just need to modify the view controller class, and the toolbar class to properly expose some properties in order to control hiding the buttons and setting an initial aspect ratio.

I'm a bit too busy to do it right now, sorry, but if you feel like trying it out yourself, I'll happily accept a pull request. :)

Thanks!

LewisTheobald commented 9 years ago

Hi @retsohuang, I've just stumbled on this for a Swift project I'm working on and wanted something very similar. I created a new method in the view controller:

- (void)setInitialAspectRatio {

    // Create the aspect ratio
    CGSize aspectRatio = CGSizeMake(1.0f, 1.0f);

    // Set the aspect ratio
    [self.cropView setAspectLockEnabledWithAspectRatio:aspectRatio animated:false];

    // Set the button as active
    self.toolbar.clampButtonGlowing = true;
}

At the end of 'viewDidLoad' just fire that off:

[self setInitialAspectRatio];

Then under the 'resetCropViewLayout' method, put the same:

[self setInitialAspectRatio];

If you open TOCropToolbar, find the five rows starting with '_clampButton' and comment them out (that'll remove the button from showing altogether if you want it removed).

retsohuang commented 9 years ago

:+1:

TimOliver commented 9 years ago

Hahah thanks a lot guys!

I'll leave this issue open to remind me to actually add proper support for this. :)

gitsakos commented 9 years ago

I just did more or less the exact same thing as @LewisTheobald. However, it seems as though rotateImageNinetyDegreesAnimated does not respect the aspectLockEnabled property and will reset the aspect ratio when rotating.

So I went a little deeper and created a property called fixedAspectRatio. Then put the following code in both -viewDidLoad and -rotateCropView.

if (!CGSizeEqualToSize(fixedAspectRatio, CGSizeZero)) {
    [self.cropView setAspectLockEnabledWithAspectRatio:fixedAspectRatio animated:NO];
}

To hide the display of the clamp and reset buttons, I added properties/methods for those buttons analogous to the existing -setRotateButtonHidden in TOCropToolbar.

TimOliver commented 8 years ago

Hi guys! The ability to lock the crop view to a specific aspect ratio has now been merged in from another contributor. Hopefully it works for you. :)