@@ -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
}
@@ -571,50 +571,28 @@ - (void)showAspectRatioDialog
571
571
572
572
// Prepare the localized options
573
573
NSString *cancelButtonTitle = NSLocalizedStringFromTableInBundle(@" Cancel" , @" TOCropViewControllerLocalizable" , resourceBundle, nil );
574
- NSString *originalButtonTitle = NSLocalizedStringFromTableInBundle(@" Original" , @" TOCropViewControllerLocalizable" , resourceBundle, nil );
575
- NSString *squareButtonTitle = NSLocalizedStringFromTableInBundle(@" Square" , @" TOCropViewControllerLocalizable" , resourceBundle, nil );
576
574
577
575
// Prepare the list that will be fed to the alert view/controller
578
-
579
- // Ratio titles according to the order of enum TOCropViewControllerAspectRatioPreset
580
- NSArray <NSString *> *portraitRatioTitles = @[originalButtonTitle, squareButtonTitle, @" 2:3" , @" 3:5" , @" 3:4" , @" 4:5" , @" 5:7" , @" 9:16" ];
581
- NSArray <NSString *> *landscapeRatioTitles = @[originalButtonTitle, squareButtonTitle, @" 3:2" , @" 5:3" , @" 4:3" , @" 5:4" , @" 7:5" , @" 16:9" ];
582
576
583
- NSMutableArray *ratioValues = [NSMutableArray array ];
584
- NSMutableArray *itemStrings = [NSMutableArray array ];
577
+ NSArray <TOCropViewControllerAspectRatioPreset *> *presets;
585
578
586
579
if (self.allowedAspectRatios == nil ) {
587
- for (NSInteger i = 0 ; i < TOCropViewControllerAspectRatioPresetCustom; i++) {
588
- NSString *itemTitle = verticalCropBox ? portraitRatioTitles[i] : landscapeRatioTitles[i];
589
- [itemStrings addObject: itemTitle];
590
- [ratioValues addObject: @(i)];
591
- }
580
+ presets = verticalCropBox ? [TOCropViewControllerAspectRatioPreset portraitPresets ] : [TOCropViewControllerAspectRatioPreset landscapePresets ];
592
581
}
593
582
else {
594
- for (NSNumber *allowedRatio in self.allowedAspectRatios ) {
595
- TOCropViewControllerAspectRatioPreset ratio = allowedRatio.integerValue ;
596
- NSString *itemTitle = verticalCropBox ? portraitRatioTitles[ratio] : landscapeRatioTitles[ratio];
597
- [itemStrings addObject: itemTitle];
598
- [ratioValues addObject: allowedRatio];
599
- }
600
- }
601
-
602
- // If a custom aspect ratio is provided, and a custom name has been given to it, add it as a visible choice
603
- if (self.customAspectRatioName .length > 0 && !CGSizeEqualToSize (CGSizeZero, self.customAspectRatio )) {
604
- [itemStrings addObject: self .customAspectRatioName];
605
- [ratioValues addObject: @(TOCropViewControllerAspectRatioPresetCustom)];
583
+ presets = self.allowedAspectRatios ;
606
584
}
607
585
608
586
UIAlertController *alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle: UIAlertControllerStyleActionSheet];
609
587
[alertController addAction: [UIAlertAction actionWithTitle: cancelButtonTitle style: UIAlertActionStyleCancel handler: nil ]];
610
588
611
589
// Add each item to the alert controller
612
- for (NSInteger i = 0 ; i < itemStrings .count ; i++) {
590
+ for (NSInteger i = 0 ; i < presets .count ; i++) {
613
591
id handlerBlock = ^(UIAlertAction *action) {
614
- [self setAspectRatioPreset: [ratioValues[i] integerValue ] animated: YES ];
592
+ [self setAspectRatioPreset: presets[i].size animated: YES ];
615
593
self.aspectRatioLockEnabled = YES ;
616
594
};
617
- UIAlertAction *action = [UIAlertAction actionWithTitle: itemStrings [i] style: UIAlertActionStyleDefault handler: handlerBlock];
595
+ UIAlertAction *action = [UIAlertAction actionWithTitle: presets [i].title style: UIAlertActionStyleDefault handler: handlerBlock];
618
596
[alertController addAction: action];
619
597
}
620
598
@@ -625,58 +603,10 @@ - (void)showAspectRatioDialog
625
603
[self presentViewController: alertController animated: YES completion: nil ];
626
604
}
627
605
628
- - (void )setAspectRatioPreset : (TOCropViewControllerAspectRatioPreset )aspectRatioPreset animated : (BOOL )animated
606
+ - (void )setAspectRatioPreset : (CGSize )aspectRatioPreset animated : (BOOL )animated
629
607
{
630
- CGSize aspectRatio = CGSizeZero;
631
-
632
608
_aspectRatioPreset = aspectRatioPreset;
633
-
634
- switch (aspectRatioPreset) {
635
- case TOCropViewControllerAspectRatioPresetOriginal:
636
- aspectRatio = CGSizeZero;
637
- break ;
638
- case TOCropViewControllerAspectRatioPresetSquare:
639
- aspectRatio = CGSizeMake (1 .0f , 1 .0f );
640
- break ;
641
- case TOCropViewControllerAspectRatioPreset3x2:
642
- aspectRatio = CGSizeMake (3 .0f , 2 .0f );
643
- break ;
644
- case TOCropViewControllerAspectRatioPreset5x3:
645
- aspectRatio = CGSizeMake (5 .0f , 3 .0f );
646
- break ;
647
- case TOCropViewControllerAspectRatioPreset4x3:
648
- aspectRatio = CGSizeMake (4 .0f , 3 .0f );
649
- break ;
650
- case TOCropViewControllerAspectRatioPreset5x4:
651
- aspectRatio = CGSizeMake (5 .0f , 4 .0f );
652
- break ;
653
- case TOCropViewControllerAspectRatioPreset7x5:
654
- aspectRatio = CGSizeMake (7 .0f , 5 .0f );
655
- break ;
656
- case TOCropViewControllerAspectRatioPreset16x9:
657
- aspectRatio = CGSizeMake (16 .0f , 9 .0f );
658
- break ;
659
- case TOCropViewControllerAspectRatioPresetCustom:
660
- aspectRatio = self.customAspectRatio ;
661
- break ;
662
- }
663
-
664
- // If the aspect ratio lock is not enabled, allow a swap
665
- // If the aspect ratio lock is on, allow a aspect ratio swap
666
- // only if the allowDimensionSwap option is specified.
667
- BOOL aspectRatioCanSwapDimensions = !self.aspectRatioLockEnabled ||
668
- (self.aspectRatioLockEnabled && self.aspectRatioLockDimensionSwapEnabled );
669
-
670
- // If the image is a portrait shape, flip the aspect ratio to match
671
- if (self.cropView .cropBoxAspectRatioIsPortrait &&
672
- aspectRatioCanSwapDimensions)
673
- {
674
- CGFloat width = aspectRatio.width ;
675
- aspectRatio.width = aspectRatio.height ;
676
- aspectRatio.height = width;
677
- }
678
-
679
- [self .cropView setAspectRatio: aspectRatio animated: animated];
609
+ [self .cropView setAspectRatio: aspectRatioPreset animated: animated];
680
610
}
681
611
682
612
- (void )rotateCropViewClockwise
@@ -1200,12 +1130,6 @@ - (void)setResetAspectRatioEnabled:(BOOL)resetAspectRatioEnabled
1200
1130
}
1201
1131
}
1202
1132
1203
- - (void )setCustomAspectRatio : (CGSize)customAspectRatio
1204
- {
1205
- _customAspectRatio = customAspectRatio;
1206
- [self setAspectRatioPreset: TOCropViewControllerAspectRatioPresetCustom animated: NO ];
1207
- }
1208
-
1209
1133
- (BOOL )resetAspectRatioEnabled
1210
1134
{
1211
1135
return self.cropView .resetAspectRatioEnabled ;
0 commit comments