Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
TimOliver committed Dec 1, 2018
2 parents 5f6a9ed + 3d618ca commit 0e75c59
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Objective-C/TOCropViewController/TOCropViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@
*/
@property (nullable, nonatomic, strong) NSArray<UIActivityType> *excludedActivityTypes;

/**
The aspect ratios which user can select in the actionsheet
*/
@property (nullable, nonatomic, strong) NSArray <NSNumber *> *allowedAspectRatios;
/**
When the user hits cancel, or completes a
UIActivityViewController operation, this block will be called,
Expand Down
33 changes: 25 additions & 8 deletions Objective-C/TOCropViewController/TOCropViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ - (instancetype)initWithCroppingStyle:(TOCropViewCroppingStyle)style image:(UIIm
// Default initial behaviour
_aspectRatioPreset = TOCropViewControllerAspectRatioPresetOriginal;
_toolbarPosition = TOCropViewControllerToolbarPositionBottom;

_allowedAspectRatios = @[@(TOCropViewControllerAspectRatioPresetOriginal),
@(TOCropViewControllerAspectRatioPresetSquare),
@(TOCropViewControllerAspectRatioPreset3x2),
@(TOCropViewControllerAspectRatioPreset5x3),
@(TOCropViewControllerAspectRatioPreset4x3),
@(TOCropViewControllerAspectRatioPreset5x4),
@(TOCropViewControllerAspectRatioPreset7x5),
@(TOCropViewControllerAspectRatioPreset16x9)];
}

return self;
Expand Down Expand Up @@ -581,14 +590,23 @@ - (void)showAspectRatioDialog
NSString *squareButtonTitle = NSLocalizedStringFromTableInBundle(@"Square", @"TOCropViewControllerLocalizable", resourceBundle, nil);

//Prepare the list that will be fed to the alert view/controller

// Ratio titles according to the order of enum TOCropViewControllerAspectRatioPreset
NSArray *portraitRatioTitles = @[originalButtonTitle, squareButtonTitle, @"2:3", @"3:5", @"3:4", @"4:5", @"5:7", @"9:16"];
NSArray *landscapeRatioTitles = @[originalButtonTitle, squareButtonTitle, @"3:2", @"5:3", @"4:3", @"5:4", @"7:5", @"16:9"];

NSMutableArray *items = [NSMutableArray array];
[items addObject:originalButtonTitle];
[items addObject:squareButtonTitle];
if (verticalCropBox) {
[items addObjectsFromArray:@[@"2:3", @"3:5", @"3:4", @"4:5", @"5:7", @"9:16"]];
for(NSNumber *aspectRatio in _allowedAspectRatios){
NSInteger index = [aspectRatio integerValue];
[items addObject:portraitRatioTitles[index]];
}
}
else {
[items addObjectsFromArray:@[@"3:2", @"5:3", @"4:3", @"5:4", @"7:5", @"16:9"]];
for(NSNumber *aspectRatio in _allowedAspectRatios){
NSInteger index = [aspectRatio integerValue];
[items addObject:landscapeRatioTitles[index]];
}
}

//Present via a UIAlertController if >= iOS 8
Expand All @@ -598,13 +616,12 @@ - (void)showAspectRatioDialog

//Add each item to the alert controller
NSInteger i = 0;
for (NSString *item in items) {
UIAlertAction *action = [UIAlertAction actionWithTitle:item style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self setAspectRatioPreset:(TOCropViewControllerAspectRatioPreset)i animated:YES];
for(NSNumber *aspectRatio in _allowedAspectRatios){
UIAlertAction *action = [UIAlertAction actionWithTitle:items[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self setAspectRatioPreset:(TOCropViewControllerAspectRatioPreset)[aspectRatio integerValue] animated:YES];
self.aspectRatioLockEnabled = YES;
}];
[alertController addAction:action];

i++;
}

Expand Down
2 changes: 1 addition & 1 deletion Objective-C/TOCropViewController/Views/TOCropView.m
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ - (void)layoutInitialImage
[self captureStateForImageRotation];

//save the size for checking if we're in a resettable state
self.originalCropBoxSize = self.resetAspectRatioEnabled ? scaledImageSize : cropBoxSize;
self.originalCropBoxSize = self.resetAspectRatioEnabled ? scaledImageSize : self.cropBoxFrame.size;
self.originalContentOffset = self.scrollView.contentOffset;

[self checkForCanReset];
Expand Down
5 changes: 5 additions & 0 deletions Objective-C/TOCropViewControllerExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking
// -- Uncomment this line of code to place the toolbar at the top of the view controller --
//cropController.toolbarPosition = TOCropViewControllerToolbarPositionTop;

// -- Uncomment this line of code to include only certain type of preset ratios
//cropController.allowedAspectRatios = @[@(TOCropViewControllerAspectRatioPresetOriginal),
// @(TOCropViewControllerAspectRatioPresetSquare),
// @(TOCropViewControllerAspectRatioPreset3x2)];

//cropController.rotateButtonsHidden = YES;
//cropController.rotateClockwiseButtonHidden = NO;

Expand Down

0 comments on commit 0e75c59

Please sign in to comment.