Skip to content

Commit 2065acc

Browse files
authored
Merge pull request #578 from TimOliver/fix-core-graphics
Remove deprecated Core Graphics renderer APIs
2 parents 4df91ec + 5cd4657 commit 2065acc

File tree

2 files changed

+42
-82
lines changed

2 files changed

+42
-82
lines changed

Objective-C/TOCropViewController/Categories/UIImage+CropRotate.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ - (BOOL)hasAlpha
3333

3434
- (UIImage *)croppedImageWithFrame:(CGRect)frame angle:(NSInteger)angle circularClip:(BOOL)circular
3535
{
36-
UIImage *croppedImage = nil;
37-
UIGraphicsBeginImageContextWithOptions(frame.size, !self.hasAlpha && !circular, self.scale);
38-
{
39-
CGContextRef context = UIGraphicsGetCurrentContext();
36+
UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat new];
37+
format.opaque = !self.hasAlpha && !circular;
38+
format.scale = self.scale;
39+
40+
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:frame.size format:format];
41+
UIImage *croppedImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) {
42+
CGContextRef context = rendererContext.CGContext;
4043

4144
// If we're capturing a circular image, set the clip mask first
4245
if (circular) {
@@ -68,10 +71,7 @@ - (UIImage *)croppedImageWithFrame:(CGRect)frame angle:(NSInteger)angle circular
6871
// We do not need to worry about specifying the size here since we're already
6972
// constrained by the context image size
7073
[self drawAtPoint:CGPointZero];
71-
72-
croppedImage = UIGraphicsGetImageFromCurrentImageContext();
73-
}
74-
UIGraphicsEndImageContext();
74+
}];
7575

7676
// Re-apply the retina scale we originally had
7777
return [UIImage imageWithCGImage:croppedImage.CGImage scale:self.scale orientation:UIImageOrientationUp];

Objective-C/TOCropViewController/Views/TOCropToolbar.m

Lines changed: 34 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -466,23 +466,16 @@ + (UIImage *)doneImage
466466
withConfiguration:[UIImageSymbolConfiguration configurationWithWeight:UIImageSymbolWeightSemibold]];
467467
}
468468

469-
UIImage *doneImage = nil;
470-
471-
UIGraphicsBeginImageContextWithOptions((CGSize){17,14}, NO, 0.0f);
472-
{
473-
//// Rectangle Drawing
469+
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:(CGSize){17,14}];
470+
UIImage *doneImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) {
474471
UIBezierPath* rectanglePath = UIBezierPath.bezierPath;
475472
[rectanglePath moveToPoint: CGPointMake(1, 7)];
476473
[rectanglePath addLineToPoint: CGPointMake(6, 12)];
477474
[rectanglePath addLineToPoint: CGPointMake(16, 1)];
478475
[UIColor.whiteColor setStroke];
479476
rectanglePath.lineWidth = 2;
480477
[rectanglePath stroke];
481-
482-
483-
doneImage = UIGraphicsGetImageFromCurrentImageContext();
484-
}
485-
UIGraphicsEndImageContext();
478+
}];
486479

487480
return doneImage;
488481
}
@@ -494,29 +487,22 @@ + (UIImage *)cancelImage
494487
withConfiguration:[UIImageSymbolConfiguration configurationWithWeight:UIImageSymbolWeightSemibold]];
495488
}
496489

497-
UIImage *cancelImage = nil;
498-
499-
UIGraphicsBeginImageContextWithOptions((CGSize){16,16}, NO, 0.0f);
500-
{
490+
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:(CGSize){16,16}];
491+
UIImage *cancelImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) {
501492
UIBezierPath* bezierPath = UIBezierPath.bezierPath;
502493
[bezierPath moveToPoint: CGPointMake(15, 15)];
503494
[bezierPath addLineToPoint: CGPointMake(1, 1)];
504495
[UIColor.whiteColor setStroke];
505496
bezierPath.lineWidth = 2;
506497
[bezierPath stroke];
507-
508-
509-
//// Bezier 2 Drawing
498+
510499
UIBezierPath* bezier2Path = UIBezierPath.bezierPath;
511500
[bezier2Path moveToPoint: CGPointMake(1, 15)];
512501
[bezier2Path addLineToPoint: CGPointMake(15, 1)];
513502
[UIColor.whiteColor setStroke];
514503
bezier2Path.lineWidth = 2;
515504
[bezier2Path stroke];
516-
517-
cancelImage = UIGraphicsGetImageFromCurrentImageContext();
518-
}
519-
UIGraphicsEndImageContext();
505+
}];
520506

521507
return cancelImage;
522508
}
@@ -529,17 +515,12 @@ + (UIImage *)rotateCCWImage
529515
imageWithBaselineOffsetFromBottom:4];
530516
}
531517

532-
UIImage *rotateImage = nil;
533-
534-
UIGraphicsBeginImageContextWithOptions((CGSize){18,21}, NO, 0.0f);
535-
{
536-
//// Rectangle 2 Drawing
518+
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:(CGSize){18,21}];
519+
UIImage *rotateImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) {
537520
UIBezierPath* rectangle2Path = [UIBezierPath bezierPathWithRect: CGRectMake(0, 9, 12, 12)];
538521
[UIColor.whiteColor setFill];
539522
[rectangle2Path fill];
540-
541-
542-
//// Rectangle 3 Drawing
523+
543524
UIBezierPath* rectangle3Path = UIBezierPath.bezierPath;
544525
[rectangle3Path moveToPoint: CGPointMake(5, 3)];
545526
[rectangle3Path addLineToPoint: CGPointMake(10, 6)];
@@ -548,19 +529,15 @@ + (UIImage *)rotateCCWImage
548529
[rectangle3Path closePath];
549530
[UIColor.whiteColor setFill];
550531
[rectangle3Path fill];
551-
552-
553-
//// Bezier Drawing
532+
554533
UIBezierPath* bezierPath = UIBezierPath.bezierPath;
555534
[bezierPath moveToPoint: CGPointMake(10, 3)];
556535
[bezierPath addCurveToPoint: CGPointMake(17.5, 11) controlPoint1: CGPointMake(15, 3) controlPoint2: CGPointMake(17.5, 5.91)];
557536
[UIColor.whiteColor setStroke];
558537
bezierPath.lineWidth = 1;
559538
[bezierPath stroke];
560-
rotateImage = UIGraphicsGetImageFromCurrentImageContext();
561-
}
562-
UIGraphicsEndImageContext();
563-
539+
}];
540+
564541
return rotateImage;
565542
}
566543

@@ -573,13 +550,14 @@ + (UIImage *)rotateCWImage
573550
}
574551

575552
UIImage *rotateCCWImage = [self.class rotateCCWImage];
576-
UIGraphicsBeginImageContextWithOptions(rotateCCWImage.size, NO, rotateCCWImage.scale);
577-
CGContextRef context = UIGraphicsGetCurrentContext();
578-
CGContextTranslateCTM(context, rotateCCWImage.size.width, rotateCCWImage.size.height);
579-
CGContextRotateCTM(context, M_PI);
580-
CGContextDrawImage(context,CGRectMake(0,0,rotateCCWImage.size.width,rotateCCWImage.size.height),rotateCCWImage.CGImage);
581-
UIImage *rotateCWImage = UIGraphicsGetImageFromCurrentImageContext();
582-
UIGraphicsEndImageContext();
553+
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:rotateCCWImage.size];
554+
UIImage *rotateCWImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) {
555+
CGContextRef context = rendererContext.CGContext;
556+
CGContextTranslateCTM(context, rotateCCWImage.size.width, rotateCCWImage.size.height);
557+
CGContextRotateCTM(context, M_PI);
558+
CGContextDrawImage(context,CGRectMake(0,0,rotateCCWImage.size.width,rotateCCWImage.size.height),rotateCCWImage.CGImage);
559+
}];
560+
583561
return rotateCWImage;
584562
}
585563

@@ -591,12 +569,8 @@ + (UIImage *)resetImage
591569
imageWithBaselineOffsetFromBottom:0];;
592570
}
593571

594-
UIImage *resetImage = nil;
595-
596-
UIGraphicsBeginImageContextWithOptions((CGSize){22,18}, NO, 0.0f);
597-
{
598-
599-
//// Bezier 2 Drawing
572+
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:(CGSize){22,18}];
573+
UIImage *resetImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) {
600574
UIBezierPath* bezier2Path = UIBezierPath.bezierPath;
601575
[bezier2Path moveToPoint: CGPointMake(22, 9)];
602576
[bezier2Path addCurveToPoint: CGPointMake(13, 18) controlPoint1: CGPointMake(22, 13.97) controlPoint2: CGPointMake(17.97, 18)];
@@ -613,9 +587,7 @@ + (UIImage *)resetImage
613587
[bezier2Path closePath];
614588
[UIColor.whiteColor setFill];
615589
[bezier2Path fill];
616-
617-
618-
//// Polygon Drawing
590+
619591
UIBezierPath* polygonPath = UIBezierPath.bezierPath;
620592
[polygonPath moveToPoint: CGPointMake(5, 15)];
621593
[polygonPath addLineToPoint: CGPointMake(10, 9)];
@@ -624,11 +596,7 @@ + (UIImage *)resetImage
624596
[polygonPath closePath];
625597
[UIColor.whiteColor setFill];
626598
[polygonPath fill];
627-
628-
629-
resetImage = UIGraphicsGetImageFromCurrentImageContext();
630-
}
631-
UIGraphicsEndImageContext();
599+
}];
632600

633601
return resetImage;
634602
}
@@ -640,46 +608,38 @@ + (UIImage *)clampImage
640608
withConfiguration:[UIImageSymbolConfiguration configurationWithWeight:UIImageSymbolWeightSemibold]]
641609
imageWithBaselineOffsetFromBottom:0];
642610
}
643-
644-
UIImage *clampImage = nil;
645611

646-
UIGraphicsBeginImageContextWithOptions((CGSize){22,16}, NO, 0.0f);
647-
{
612+
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:(CGSize){22,16}];
613+
UIImage *clampImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) {
648614
//// Color Declarations
649615
UIColor* outerBox = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.553];
650616
UIColor* innerBox = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.773];
651-
617+
652618
//// Rectangle Drawing
653619
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 3, 13, 13)];
654620
[UIColor.whiteColor setFill];
655621
[rectanglePath fill];
656-
657-
622+
658623
//// Outer
659624
{
660625
//// Top Drawing
661626
UIBezierPath* topPath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 0, 22, 2)];
662627
[outerBox setFill];
663628
[topPath fill];
664-
665-
629+
630+
666631
//// Side Drawing
667632
UIBezierPath* sidePath = [UIBezierPath bezierPathWithRect: CGRectMake(19, 2, 3, 14)];
668633
[outerBox setFill];
669634
[sidePath fill];
670635
}
671-
672-
636+
673637
//// Rectangle 2 Drawing
674638
UIBezierPath* rectangle2Path = [UIBezierPath bezierPathWithRect: CGRectMake(14, 3, 4, 13)];
675639
[innerBox setFill];
676640
[rectangle2Path fill];
677-
678-
679-
clampImage = UIGraphicsGetImageFromCurrentImageContext();
680-
}
681-
UIGraphicsEndImageContext();
682-
641+
}];
642+
683643
return clampImage;
684644
}
685645

0 commit comments

Comments
 (0)