@@ -90,7 +90,7 @@ - (instancetype)initWithCroppingStyle:(TOCropViewCroppingStyle)style image:(UIIm
90
90
_transitionController = [[TOCropViewControllerTransitioning alloc ] init ];
91
91
92
92
// Default initial behaviour
93
- _aspectRatioPreset = TOCropViewControllerAspectRatioPresetOriginal ;
93
+ _aspectRatioPreset = CGSizeZero ;
94
94
95
95
#if TARGET_OS_MACCATALYST
96
96
_toolbarPosition = TOCropViewControllerToolbarPositionTop;
@@ -170,7 +170,7 @@ - (void)viewWillAppear:(BOOL)animated
170
170
171
171
// If an initial aspect ratio was set before presentation, set it now once the rest of
172
172
// the setup will have been done
173
- if (self.aspectRatioPreset != TOCropViewControllerAspectRatioPresetOriginal ) {
173
+ if (! CGSizeEqualToSize ( self.aspectRatioPreset , CGSizeZero) ) {
174
174
[self setAspectRatioPreset: self .aspectRatioPreset animated: NO ];
175
175
}
176
176
}
@@ -586,50 +586,28 @@ - (void)showAspectRatioDialog
586
586
587
587
// Prepare the localized options
588
588
NSString *cancelButtonTitle = NSLocalizedStringFromTableInBundle(@" Cancel" , @" TOCropViewControllerLocalizable" , resourceBundle, nil );
589
- NSString *originalButtonTitle = NSLocalizedStringFromTableInBundle(@" Original" , @" TOCropViewControllerLocalizable" , resourceBundle, nil );
590
- NSString *squareButtonTitle = NSLocalizedStringFromTableInBundle(@" Square" , @" TOCropViewControllerLocalizable" , resourceBundle, nil );
591
589
592
590
// Prepare the list that will be fed to the alert view/controller
593
-
594
- // Ratio titles according to the order of enum TOCropViewControllerAspectRatioPreset
595
- NSArray <NSString *> *portraitRatioTitles = @[originalButtonTitle, squareButtonTitle, @" 2:3" , @" 3:5" , @" 3:4" , @" 4:5" , @" 5:7" , @" 9:16" ];
596
- NSArray <NSString *> *landscapeRatioTitles = @[originalButtonTitle, squareButtonTitle, @" 3:2" , @" 5:3" , @" 4:3" , @" 5:4" , @" 7:5" , @" 16:9" ];
597
591
598
- NSMutableArray *ratioValues = [NSMutableArray array ];
599
- NSMutableArray *itemStrings = [NSMutableArray array ];
592
+ NSArray <TOCropViewControllerAspectRatioPreset *> *presets;
600
593
601
594
if (self.allowedAspectRatios == nil ) {
602
- for (NSInteger i = 0 ; i < TOCropViewControllerAspectRatioPresetCustom; i++) {
603
- NSString *itemTitle = verticalCropBox ? portraitRatioTitles[i] : landscapeRatioTitles[i];
604
- [itemStrings addObject: itemTitle];
605
- [ratioValues addObject: @(i)];
606
- }
595
+ presets = verticalCropBox ? [TOCropViewControllerAspectRatioPreset portraitPresets ] : [TOCropViewControllerAspectRatioPreset landscapePresets ];
607
596
}
608
597
else {
609
- for (NSNumber *allowedRatio in self.allowedAspectRatios ) {
610
- TOCropViewControllerAspectRatioPreset ratio = allowedRatio.integerValue ;
611
- NSString *itemTitle = verticalCropBox ? portraitRatioTitles[ratio] : landscapeRatioTitles[ratio];
612
- [itemStrings addObject: itemTitle];
613
- [ratioValues addObject: allowedRatio];
614
- }
615
- }
616
-
617
- // If a custom aspect ratio is provided, and a custom name has been given to it, add it as a visible choice
618
- if (self.customAspectRatioName .length > 0 && !CGSizeEqualToSize (CGSizeZero, self.customAspectRatio )) {
619
- [itemStrings addObject: self .customAspectRatioName];
620
- [ratioValues addObject: @(TOCropViewControllerAspectRatioPresetCustom)];
598
+ presets = self.allowedAspectRatios ;
621
599
}
622
600
623
601
UIAlertController *alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle: UIAlertControllerStyleActionSheet];
624
602
[alertController addAction: [UIAlertAction actionWithTitle: cancelButtonTitle style: UIAlertActionStyleCancel handler: nil ]];
625
603
626
604
// Add each item to the alert controller
627
- for (NSInteger i = 0 ; i < itemStrings .count ; i++) {
605
+ for (NSInteger i = 0 ; i < presets .count ; i++) {
628
606
id handlerBlock = ^(UIAlertAction *action) {
629
- [self setAspectRatioPreset: [ratioValues[i] integerValue ] animated: YES ];
607
+ [self setAspectRatioPreset: presets[i].size animated: YES ];
630
608
self.aspectRatioLockEnabled = YES ;
631
609
};
632
- UIAlertAction *action = [UIAlertAction actionWithTitle: itemStrings [i] style: UIAlertActionStyleDefault handler: handlerBlock];
610
+ UIAlertAction *action = [UIAlertAction actionWithTitle: presets [i].title style: UIAlertActionStyleDefault handler: handlerBlock];
633
611
[alertController addAction: action];
634
612
}
635
613
@@ -640,58 +618,10 @@ - (void)showAspectRatioDialog
640
618
[self presentViewController: alertController animated: YES completion: nil ];
641
619
}
642
620
643
- - (void )setAspectRatioPreset : (TOCropViewControllerAspectRatioPreset )aspectRatioPreset animated : (BOOL )animated
621
+ - (void )setAspectRatioPreset : (CGSize )aspectRatioPreset animated : (BOOL )animated
644
622
{
645
- CGSize aspectRatio = CGSizeZero;
646
-
647
623
_aspectRatioPreset = aspectRatioPreset;
648
-
649
- switch (aspectRatioPreset) {
650
- case TOCropViewControllerAspectRatioPresetOriginal:
651
- aspectRatio = CGSizeZero;
652
- break ;
653
- case TOCropViewControllerAspectRatioPresetSquare:
654
- aspectRatio = CGSizeMake (1 .0f , 1 .0f );
655
- break ;
656
- case TOCropViewControllerAspectRatioPreset3x2:
657
- aspectRatio = CGSizeMake (3 .0f , 2 .0f );
658
- break ;
659
- case TOCropViewControllerAspectRatioPreset5x3:
660
- aspectRatio = CGSizeMake (5 .0f , 3 .0f );
661
- break ;
662
- case TOCropViewControllerAspectRatioPreset4x3:
663
- aspectRatio = CGSizeMake (4 .0f , 3 .0f );
664
- break ;
665
- case TOCropViewControllerAspectRatioPreset5x4:
666
- aspectRatio = CGSizeMake (5 .0f , 4 .0f );
667
- break ;
668
- case TOCropViewControllerAspectRatioPreset7x5:
669
- aspectRatio = CGSizeMake (7 .0f , 5 .0f );
670
- break ;
671
- case TOCropViewControllerAspectRatioPreset16x9:
672
- aspectRatio = CGSizeMake (16 .0f , 9 .0f );
673
- break ;
674
- case TOCropViewControllerAspectRatioPresetCustom:
675
- aspectRatio = self.customAspectRatio ;
676
- break ;
677
- }
678
-
679
- // If the aspect ratio lock is not enabled, allow a swap
680
- // If the aspect ratio lock is on, allow a aspect ratio swap
681
- // only if the allowDimensionSwap option is specified.
682
- BOOL aspectRatioCanSwapDimensions = !self.aspectRatioLockEnabled ||
683
- (self.aspectRatioLockEnabled && self.aspectRatioLockDimensionSwapEnabled );
684
-
685
- // If the image is a portrait shape, flip the aspect ratio to match
686
- if (self.cropView .cropBoxAspectRatioIsPortrait &&
687
- aspectRatioCanSwapDimensions)
688
- {
689
- CGFloat width = aspectRatio.width ;
690
- aspectRatio.width = aspectRatio.height ;
691
- aspectRatio.height = width;
692
- }
693
-
694
- [self .cropView setAspectRatio: aspectRatio animated: animated];
624
+ [self .cropView setAspectRatio: aspectRatioPreset animated: animated];
695
625
}
696
626
697
627
- (void )rotateCropViewClockwise
@@ -1215,12 +1145,6 @@ - (void)setResetAspectRatioEnabled:(BOOL)resetAspectRatioEnabled
1215
1145
}
1216
1146
}
1217
1147
1218
- - (void )setCustomAspectRatio : (CGSize)customAspectRatio
1219
- {
1220
- _customAspectRatio = customAspectRatio;
1221
- [self setAspectRatioPreset: TOCropViewControllerAspectRatioPresetCustom animated: NO ];
1222
- }
1223
-
1224
1148
- (BOOL )resetAspectRatioEnabled
1225
1149
{
1226
1150
return self.cropView .resetAspectRatioEnabled ;
0 commit comments