Skip to content

Commit b398b9a

Browse files
Replace obsolete iOS BeginImageContextWithOptions (dotnet#28682)
* Replace obsolete iOS BeginImageContextWithOptions * Update FormsCheckBox.cs * Update FormsCheckBox.cs * BeginImageContextWithOptions changes --------- Co-authored-by: Tamilarasan Paranthaman <93904422+Tamilarasan-Paranthaman@users.noreply.github.com>
1 parent a835905 commit b398b9a

File tree

17 files changed

+319
-220
lines changed

17 files changed

+319
-220
lines changed

src/Compatibility/Core/src/iOS/Renderers/FormsCheckBox.cs

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -176,60 +176,65 @@ internal virtual void DrawCheckMark(UIBezierPath path)
176176

177177
internal virtual UIImage CreateCheckBox(UIImage check)
178178
{
179-
UIGraphics.BeginImageContextWithOptions(new CGSize(_defaultSize, _defaultSize), false, 0);
180-
var context = UIGraphics.GetCurrentContext();
181-
context.SaveState();
179+
var renderer = new UIGraphicsImageRenderer(new CGSize(_defaultSize, _defaultSize), new UIGraphicsImageRendererFormat()
180+
{
181+
Opaque = false,
182+
Scale = 0,
183+
});
182184

183-
var checkedColor = CheckBoxTintUIColor;
184-
checkedColor.SetFill();
185-
checkedColor.SetStroke();
185+
return renderer.CreateImage((context) =>
186+
{
187+
context.CGContext.SaveState();
186188

187-
var vPadding = _lineWidth / 2;
188-
var hPadding = _lineWidth / 2;
189-
var diameter = _defaultSize - _lineWidth;
189+
var checkedColor = CheckBoxTintUIColor;
190+
checkedColor.SetFill();
191+
checkedColor.SetStroke();
190192

191-
var backgroundRect = new CGRect(hPadding, vPadding, diameter, diameter);
192-
var boxPath = CreateBoxPath(backgroundRect);
193-
boxPath.LineWidth = _lineWidth;
194-
boxPath.Stroke();
193+
var vPadding = _lineWidth / 2;
194+
var hPadding = _lineWidth / 2;
195+
var diameter = _defaultSize - _lineWidth;
195196

196-
if (check != null)
197-
{
198-
boxPath.Fill();
199-
check.Draw(new CGPoint(0, 0), CGBlendMode.DestinationOut, 1);
200-
}
197+
var backgroundRect = new CGRect(hPadding, vPadding, diameter, diameter);
198+
var boxPath = CreateBoxPath(backgroundRect);
199+
200+
boxPath.LineWidth = _lineWidth;
201+
boxPath.Stroke();
201202

202-
context.RestoreState();
203-
var img = UIGraphics.GetImageFromCurrentImageContext();
204-
UIGraphics.EndImageContext();
203+
if (check is not null)
204+
{
205+
boxPath.Fill();
206+
check.Draw(new CGPoint(0, 0), CGBlendMode.DestinationOut, 1);
207+
}
205208

206-
return img;
209+
context.CGContext.RestoreState();
210+
});
207211
}
208212

209213

210214
internal UIImage CreateCheckMark()
211215
{
212-
UIGraphics.BeginImageContextWithOptions(new CGSize(_defaultSize, _defaultSize), false, 0);
213-
var context = UIGraphics.GetCurrentContext();
214-
context.SaveState();
215-
216-
var vPadding = _lineWidth / 2;
217-
var hPadding = _lineWidth / 2;
218-
var diameter = _defaultSize - _lineWidth;
219-
220-
var checkPath = CreateCheckPath();
221-
222-
context.TranslateCTM(hPadding + (nfloat)(0.05 * diameter), vPadding + (nfloat)(0.1 * diameter));
223-
context.ScaleCTM(diameter, diameter);
224-
DrawCheckMark(checkPath);
225-
UIColor.White.SetStroke();
226-
checkPath.Stroke();
227-
228-
context.RestoreState();
229-
var img = UIGraphics.GetImageFromCurrentImageContext();
230-
UIGraphics.EndImageContext();
216+
var renderer = new UIGraphicsImageRenderer(new CGSize(_defaultSize, _defaultSize), new UIGraphicsImageRendererFormat()
217+
{
218+
Opaque = false,
219+
Scale = 0,
220+
});
231221

232-
return img;
222+
return renderer.CreateImage((context) =>
223+
{
224+
context.CGContext.SaveState();
225+
226+
var vPadding = _lineWidth / 2;
227+
var hPadding = _lineWidth / 2;
228+
var diameter = _defaultSize - _lineWidth;
229+
230+
var checkPath = CreateCheckPath();
231+
context.CGContext.TranslateCTM(hPadding + (nfloat)(0.05 * diameter), vPadding + (nfloat)(0.1 * diameter));
232+
context.CGContext.ScaleCTM(diameter, diameter);
233+
DrawCheckMark(checkPath);
234+
UIColor.White.SetStroke();
235+
checkPath.Stroke();
236+
context.CGContext.RestoreState();
237+
});
233238
}
234239

235240
protected override void Dispose(bool disposing)

src/Compatibility/Core/src/iOS/Renderers/ImageRenderer.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,22 @@ public Task<UIImage> LoadImageAsync(
255255
var attString = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToPlatform());
256256
var imagesize = ((NSString)fontsource.Glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _));
257257

258-
UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f);
259-
var ctx = new NSStringDrawingContext();
260-
var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx);
261-
attString.DrawString(new RectangleF(
262-
imagesize.Width / 2 - boundingRect.Size.Width / 2,
263-
imagesize.Height / 2 - boundingRect.Size.Height / 2,
264-
imagesize.Width,
265-
imagesize.Height));
266-
image = UIGraphics.GetImageFromCurrentImageContext();
267-
UIGraphics.EndImageContext();
258+
var renderer = new UIGraphicsImageRenderer(imagesize, new UIGraphicsImageRendererFormat()
259+
{
260+
Opaque = false,
261+
Scale = 0,
262+
});
263+
264+
image = renderer.CreateImage((context) =>
265+
{
266+
var ctx = new NSStringDrawingContext();
267+
var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx);
268+
attString.DrawString(new RectangleF(
269+
imagesize.Width / 2 - boundingRect.Size.Width / 2,
270+
imagesize.Height / 2 - boundingRect.Size.Height / 2,
271+
imagesize.Width,
272+
imagesize.Height));
273+
});
268274

269275
if (image != null && iconcolor != _defaultColor)
270276
image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

src/Compatibility/Core/src/iOS/Renderers/SwipeViewRenderer.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,12 +724,16 @@ UIImage MaxResizeSwipeItemIconImage(UIImage sourceImage, nfloat maxWidth, nfloat
724724

725725
var width = maxResizeFactor * sourceSize.Width;
726726
var height = maxResizeFactor * sourceSize.Height;
727-
UIGraphics.BeginImageContextWithOptions(new CGSize((nfloat)width, (nfloat)height), false, 0);
728-
sourceImage.Draw(new CGRect(0, 0, (nfloat)width, (nfloat)height));
729-
var resultImage = UIGraphics.GetImageFromCurrentImageContext();
730-
UIGraphics.EndImageContext();
727+
var renderer = new UIGraphicsImageRenderer(new CGSize((nfloat)width, (nfloat)height), new UIGraphicsImageRendererFormat()
728+
{
729+
Opaque = false,
730+
Scale = 0,
731+
});
731732

732-
return resultImage;
733+
return renderer.CreateImage((context) =>
734+
{
735+
sourceImage.Draw(new CGRect(0, 0, (nfloat)width, (nfloat)height));
736+
});
733737
}
734738

735739
void HandleTouchInteractions(GestureStatus status, CGPoint point)

src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellPageRendererTracker.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -459,30 +459,37 @@ UIImage DrawHamburger()
459459
const string hamburgerKey = "Hamburger";
460460
UIImage img = (UIImage)_nSCache.ObjectForKey((NSString)hamburgerKey);
461461

462-
if (img != null)
462+
if (img is not null)
463+
{
463464
return img;
465+
}
464466

465467
var rect = new CGRect(0, 0, 23f, 23f);
466468

467-
UIGraphics.BeginImageContextWithOptions(rect.Size, false, 0);
468-
var ctx = UIGraphics.GetCurrentContext();
469-
ctx.SaveState();
470-
ctx.SetStrokeColor(UIColor.Blue.CGColor);
471-
472-
float size = 3f;
473-
float start = 4f;
474-
ctx.SetLineWidth(size);
469+
var renderer = new UIGraphicsImageRenderer(rect.Size, new UIGraphicsImageRendererFormat()
470+
{
471+
Opaque = false,
472+
Scale = 0,
473+
});
475474

476-
for (int i = 0; i < 3; i++)
475+
img = renderer.CreateImage((context) =>
477476
{
478-
ctx.MoveTo(1f, start + i * (size * 2));
479-
ctx.AddLineToPoint(22f, start + i * (size * 2));
480-
ctx.StrokePath();
481-
}
477+
context.CGContext.SaveState();
478+
UIColor.Blue.SetStroke();
482479

483-
ctx.RestoreState();
484-
img = UIGraphics.GetImageFromCurrentImageContext();
485-
UIGraphics.EndImageContext();
480+
float size = 3f;
481+
float start = 4f;
482+
context.CGContext.SetLineWidth(size);
483+
484+
for (int i = 0; i < 3; i++)
485+
{
486+
context.CGContext.MoveTo(1f, start + i * (size * 2));
487+
context.CGContext.AddLineToPoint(22f, start + i * (size * 2));
488+
context.CGContext.StrokePath();
489+
}
490+
491+
context.CGContext.RestoreState();
492+
});
486493

487494
_nSCache.SetObjectforKey(img, (NSString)hamburgerKey);
488495
return img;

src/Controls/src/Core/Platform/iOS/Extensions/BrushExtensions.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,25 @@ public static CALayer GetBackgroundLayer(this UIView control, Brush brush)
113113

114114
public static UIImage GetBackgroundImage(this UIView control, Brush brush)
115115
{
116-
if (control == null || brush == null || brush.IsEmpty)
116+
if (control is null || brush is null || brush.IsEmpty)
117+
{
117118
return null;
119+
}
118120

119121
var backgroundLayer = control.GetBackgroundLayer(brush);
120122

121-
if (backgroundLayer == null)
122-
return null;
123-
124-
UIGraphics.BeginImageContextWithOptions(backgroundLayer.Bounds.Size, false, UIScreen.MainScreen.Scale);
125-
126-
if (UIGraphics.GetCurrentContext() == null)
123+
if (backgroundLayer is null)
124+
{
127125
return null;
126+
}
128127

129-
backgroundLayer.RenderInContext(UIGraphics.GetCurrentContext());
130-
UIImage gradientImage = UIGraphics.GetImageFromCurrentImageContext();
131-
UIGraphics.EndImageContext();
128+
var renderer = new UIGraphicsImageRenderer(backgroundLayer.Bounds.Size, new UIGraphicsImageRendererFormat()
129+
{
130+
Opaque = false,
131+
Scale = UIScreen.MainScreen.Scale,
132+
});
132133

133-
return gradientImage;
134+
return renderer.CreateImage((context) => backgroundLayer.RenderInContext(context.CGContext));
134135
}
135136

136137
public static void InsertBackgroundLayer(this UIView view, CALayer backgroundLayer, int index = -1)

src/Core/src/Handlers/SwipeItemMenuItem/SwipeItemMenuItemHandler.iOS.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,12 @@ static UIImage MaxResizeSwipeItemIconImage(UIImage sourceImage, nfloat maxWidth,
138138
var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
139139

140140
if (maxResizeFactor > 1)
141+
{
141142
return sourceImage;
143+
}
142144

143145
var width = maxResizeFactor * sourceSize.Width;
144146
var height = maxResizeFactor * sourceSize.Height;
145-
if (!OperatingSystem.IsIOSVersionAtLeast(17))
146-
{
147-
UIGraphics.BeginImageContextWithOptions(new CGSize((nfloat)width, (nfloat)height), false, 0);
148-
sourceImage.Draw(new CGRect(0, 0, (nfloat)width, (nfloat)height));
149-
var resultImage = UIGraphics.GetImageFromCurrentImageContext();
150-
UIGraphics.EndImageContext();
151-
152-
return resultImage;
153-
}
154147

155148
var format = new UIGraphicsImageRendererFormat
156149
{

src/Core/src/ImageSources/iOS/ImageSourceExtensions.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,26 @@ public static partial class ImageSourceExtensions
3535
return null;
3636
}
3737

38-
UIGraphics.BeginImageContextWithOptions(imagesize, false, scale);
39-
var ctx = new NSStringDrawingContext();
38+
var renderer = new UIGraphicsImageRenderer(imagesize, new UIGraphicsImageRendererFormat()
39+
{
40+
Opaque = false,
41+
Scale = scale,
42+
});
4043

41-
var boundingRect = attString.GetBoundingRect(imagesize, 0, ctx);
42-
attString.DrawString(new CGRect(
43-
imagesize.Width / 2 - boundingRect.Size.Width / 2,
44-
imagesize.Height / 2 - boundingRect.Size.Height / 2,
45-
imagesize.Width,
46-
imagesize.Height));
44+
return renderer.CreateImage((context) =>
45+
{
46+
var ctx = new NSStringDrawingContext();
4747

48-
var image = UIGraphics.GetImageFromCurrentImageContext();
49-
UIGraphics.EndImageContext();
48+
var boundingRect = attString.GetBoundingRect(imagesize, 0, ctx);
49+
attString.DrawString(new CGRect(
50+
imagesize.Width / 2 - boundingRect.Size.Width / 2,
51+
imagesize.Height / 2 - boundingRect.Size.Height / 2,
52+
imagesize.Width,
53+
imagesize.Height));
5054

51-
// Using UIRenderingMode.Automatic when the FontImageSource color is null (where 'Automatic' adapts based on the context and properly applies color)
55+
// Using UIRenderingMode.Automatic when the FontImageSource color is null (where 'Automatic' adapts based on the context and properly applies color)
5256
// and UIRenderingMode.AlwaysOriginal when the FontImageSource color is specified ensures the given color is applied correctly
53-
return image.ImageWithRenderingMode(imageSource.Color == null ? UIImageRenderingMode.Automatic : UIImageRenderingMode.AlwaysOriginal);
57+
}).ImageWithRenderingMode(imageSource.Color == null ? UIImageRenderingMode.Automatic : UIImageRenderingMode.AlwaysOriginal);
5458
}
5559

5660
internal static UIImage? GetPlatformImage(this IFileImageSource imageSource)

src/Core/src/Platform/iOS/TextFieldExtensions.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,26 +223,25 @@ internal static void UpdateClearButtonColor(this UITextField textField, IEntry e
223223
{
224224
var size = image.Size;
225225

226-
UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale);
226+
var renderer = new UIGraphicsImageRenderer(size, new UIGraphicsImageRendererFormat()
227+
{
228+
Opaque = false,
229+
Scale = UIScreen.MainScreen.Scale,
230+
});
227231

228-
if (UIGraphics.GetCurrentContext() == null)
232+
if (renderer is null)
233+
{
229234
return null;
235+
}
230236

231-
var context = UIGraphics.GetCurrentContext();
232-
233-
image.Draw(CGPoint.Empty, CGBlendMode.Normal, 1.0f);
234-
context?.SetFillColor(color.CGColor);
235-
context?.SetBlendMode(CGBlendMode.SourceIn);
236-
context?.SetAlpha(1.0f);
237-
238-
var rect = new CGRect(CGPoint.Empty.X, CGPoint.Empty.Y, image.Size.Width, image.Size.Height);
239-
context?.FillRect(rect);
240-
241-
var tintedImage = UIGraphics.GetImageFromCurrentImageContext();
242-
243-
UIGraphics.EndImageContext();
237+
return renderer.CreateImage((context) =>
238+
{
239+
image.Draw(CGPoint.Empty, CGBlendMode.Normal, 1.0f);
240+
color.ColorWithAlpha(1.0f).SetFill();
244241

245-
return tintedImage;
242+
var rect = new CGRect(CGPoint.Empty.X, CGPoint.Empty.Y, image.Size.Width, image.Size.Height);
243+
context?.FillRect(rect, CGBlendMode.SourceIn);
244+
});
246245
}
247246
}
248247
}

0 commit comments

Comments
 (0)