iOS Guides | Samples

MonoTouch.UIKit.UIGraphicsRenderer Class

A color-managed rendering context.

See Also: UIGraphicsRenderer

Syntax

[MonoTouch.Foundation.Register("UIGraphicsRenderer", true)]
[MonoTouch.ObjCRuntime.Introduced(MonoTouch.ObjCRuntime.PlatformName.iOS, 10, 0, MonoTouch.ObjCRuntime.PlatformArchitecture.None, null)]
[MonoTouch.ObjCRuntime.Introduced(MonoTouch.ObjCRuntime.PlatformName.TvOS, 10, 0, MonoTouch.ObjCRuntime.PlatformArchitecture.None, null)]
public abstract class UIGraphicsRenderer : NSObject

Remarks

Introduced in iOS 10, UIGraphicsRenderer and its subclasses (UIGraphicsImageRenderer and UIGraphicsPDFRenderer) allow custom drawing code that utilitizes wide color.

In versions prior to iOS 10, custom drawing was done by retrieving a CGContext, manipulating its state, and then retrieving the custom image. Now, the standard technique is to use UIGraphicsImageRenderer.CreateImage or UIGraphicsPDFRenderer.CreatePDF, both of which take an Action as an argument, allowing the developer to either pass in drawing code or use a lambda expression:

C# Example

// Pre-iOS 10 drawing pattern
var size = new CGSize (100, 100);
UIGraphics.BeginImageContext (size);

// ... drawing statements here ...

UIGraphics.EndImageContext ();
image = UIGraphics.GetImageFromCurrentImageContext ();

// iOS 10 drawing model:

var r = new UIGraphicsImageRenderer(new CGSize(100, 100));

var img = r.CreateImage((UIGraphicsImageRendererContext ctxt) => 
    {
        var superRed = UIColor.FromDisplayP3(1.358f, -0.074f, -0.012f, 1.0f);
        superRed.SetFill();

        UIColor.Black.SetStroke();

        var path = new UIBezierPath();
        path.MoveTo(new CGPoint(10,10));
        path.AddLineTo(new CGPoint(90, 10));
        path.AddLineTo(new CGPoint(45, 90));
        path.ClosePath();
        path.Stroke();
        path.Fill();
    });

The new drawing method is still based on manipulating the state of a "current context" but instead of a CGContext it is now a subclass of UIGraphicsRendererContext (either UIGraphicsImageRendererContext or UIGraphicsPDFRendererContext). If necessary, developers can retrieve the current context as a CGContext using UIGraphicsRenderer.GetContext.

Related content

Requirements

Namespace: MonoTouch.UIKit
Assembly: monotouch (in monotouch.dll)
Assembly Versions: 0.0.0.0

The members of MonoTouch.UIKit.UIGraphicsRenderer are listed below.

See Also: NSObject

Public Constructors

Default constructor.
A constructor that initializes the object from the data stored in the unarchiver object.
Constructor to call on derived classes to skip initialization and merely allocate the object.
Constructs a UIGraphicsRenderer that will render an image of the specified bounds.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.
Constructs a UIGraphicsRenderer that will render graphics of the size of bounds, using format.

Public Properties

[read-only]
AllowsImageOutputBoolean. Whether this renderer outputs an image.
[read-only]
override
ClassHandleIntPtr. The handle for this class.
[read-only]
FormatUIGraphicsRendererFormat.
[read-only]
static
RendererContextClassClass.

Public Methods