• Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A drop-in UIGraphicsRenderer port -- CrossPlatform, Swift 4, Image & PDF

GraphicsRenderer

Carthage compatible Version License Language Platform

Installation

Cocoapods

pod "GraphicsRenderer", "1.3.0"

Carthage

github "shaps80/GraphicsRenderer" ~> 1.2.1

Swift 3

pod "GraphicsRenderer", "1.1.0"

Swift 2.3

pod "GraphicsRenderer", "1.0.0"

Introduction

GraphicsRenderer is designed to a drop-in UIGraphicsRenderer port. For this reason, all function names are matched to make it easy to swap out a later date.

UIGraphicsRendererFormat > RendererFormat
UIGraphicsImageRendererFormat > ImageRendererFormat
UIGraphicsPDFRendererFormat > PDFRendererFormat

UIGraphicsRendererContext > RendererContext
UIGraphicsImageRendererContext > ImageRendererContext
UIGraphicsPDFRendererContext > PDFRendererContext

The classes you'll mostly work with though are:

UIGraphicsRenderer > Renderer
UIGraphicsImageRenderer > ImageRenderer
UIGraphicsPDFRenderer > PDFRenderer

GraphicsRenderer is also cross-platform with iOS and macOS demo projects included in the repo.

GraphicsRenderer matches the entire API currently available from UIGraphicsRenderer, however to make it work nicely with all platforms, it also includes some additional convenience's, like flipping the context.

GraphicsRenderer is also protocol based, which makes it more Swifty and allows for some nice generics driven integration as you can see in the performDrawing() example.

InkKit

I have another library called InkKit which now uses this library for its inner workings. For a LOT more drawing and layout convenience's -- checkout that library too.~~

Note: If you include InkKit in your project, you don't need to include this project too.

Example

There are 2 example projects included in the repo. One for iOS and another for OSX.

Simply select the appropriate scheme, build and run.

Lets start by creating a simple drawing function:

func performDrawing<Context: RendererContext>(context: Context) {
	let rect = context.format.bounds
    UIColor.white.setFill()
    context.fill(rect)
    
    UIColor.blue.setStroke()
    let frame = CGRect(x: 10, y: 10, width: 40, height: 40)
    context.stroke(frame)
    
    UIColor.red.setStroke()
    context.stroke(rect.insetBy(dx: 5, dy: 5))
}

Now lets create an image from this drawing:

let image = ImageRenderer(size: CGSize(width: 100, height: 100)).image { context in
	performDrawing()
}

Or perhaps you'd prefer a PDF?

let bounds = CGRect(x: 0, y: 0, width: 612, height: 792)
try? PDFRenderer(bounds: bounds).writePDF(to: url) { context in
    context.beginPage()
    performDrawing(context: context)
    context.beginPage()
    performDrawing(context: context)
}

Drawing

When working with PDFs you don't need to worry about creating the PDF, ending pages or even closing the PDF. This is all handled automatically for you.

The context returned to you inside the drawing block holds onto 2 key pieces of information. (Just like UIGraphicsRendererContext)

format -- Provides information about bounds, scale, etc.. cgContext -- The underlying CGContext

Final note, the stroke methods are optimized to work the same way as the Apple implementation, in that they automatically insetBy 0.5 for you. If you don't want this behavious automatically, simply use the usual methods available on CGContext.

e.g. cgContext.stroke(rect: myRect)

Requirements

The library has the following requirements:

  • Swift 4.0
  • iOS 8.0+
  • OSX 10.10+

Author

Shaps Benkau, [email protected]

License

GraphicsRenderer is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

Peek

All new design. Inspect your iOS application at runtime.
Swift
2,601
star
2

SwiftUIBackports

A collection of SwiftUI backports for iOS, macOS, tvOS and watchOS
Swift
940
star
3

InkKit

Drawing and Geometry made easy on iOS - now in Swift 3.0
Swift
373
star
4

iMessageStyleReveal

Adds iMessage style timestamp support. Written in Swift!
Swift
147
star
5

MarkdownText

A native SwiftUI view for rendering Markdown text in an iOS or macOS app
Swift
142
star
6

Warrant

Data validation made easy. In code and from Interface Builder.
Swift
54
star
7

Stack

A Type-Safe, Thread-Safe-ish approach to CoreData in Swift
Swift
48
star
8

SwiftUIPlus

SwiftUI additions and helpers that are missing from the official implementation.
Swift
24
star
9

Color

A Swifty type-safe abstraction around color models
Objective-C
23
star
10

Feedback

Familiar, easy state-driven haptics, audio and more
Swift
22
star
11

ColumnView

A column-view navigation controller that behaves similarly to Files and Finder
Swift
21
star
12

Logging

Apple's SwiftLog + OSLog style StringInterpolation
Swift
16
star
13

Objective-C-Style-Guide

Objective-C Style Guide
15
star
14

SwiftUIBackportsDemo

A collection of Demo's for the SwiftUI Backports.
Swift
15
star
15

SwiftBackports

A collection of Swift backports for earlier versions of iOS, macOS, tvOS and watchOS
Swift
13
star
16

Abracadabra

A truly plug 'n' play solution for securing your code.
Objective-C
12
star
17

Snaply

Drop-in snapping behaviour for any scroll/collection/table/view using arbitrary points!
Swift
9
star
18

CornerMasking

SPXMasking is category on CALayer that allows you to specify different a corner radius for each corner of a CALayer.
Objective-C
8
star
19

MarkdownTextDemo

A demo app for showcasing the MarkdownText library.
Swift
7
star
20

SVGRenderer

An SVG renderer using the familiar GraphicsRenderer API.
Swift
6
star
21

ActivityView

Moved to SwiftUI-Plus/ActivityView
5
star
22

Storage

Moved to SwiftUI-Plus/DefaultStorage
5
star
23

Media

Moved to SwiftUI-Plus/Media
5
star
24

ghost-app-extract

Allows you to create better excerpt's in your Ghost blog posts.
JavaScript
4
star
25

AppManifest

Swift
4
star
26

SwiftLayout

A Swift library for programmatically dealing with AutoLayout
Swift
4
star
27

FlowLayout-Demo

A high-performance flow layout that provides global headers, footers, section backgrounds and various configurations.
Swift
4
star
28

Endpoints

A generic Swift networking library inspired by SwiftUI APIs
Swift
4
star
29

Analytics

Swift
3
star
30

PhotoLibrary

Photo library picker for iOS
Swift
2
star
31

primo

The first script to install a newly installed Mac! This will install common terminal utilities & applications.
Shell
2
star
32

Whos-Who

Who's Who sample code for job interview.
Objective-C
2
star
33

SPXLayout

AutoLayout the programmatic way
Objective-C
2
star
34

SPXSubscripting

This is a library for adding subscripting support to various Foundation classes. It also adds some interesting solutions to working with NSString's, NSAttributedString's and their attributes.
Objective-C
2
star
35

SPXDataSources

DataSource implementations for UITableView and UICollection, plus more...
Objective-C
2
star
36

Populate

Data providers for populating your data views.
Swift
2
star
37

bash

Various bash scripts I've written to simply my development and sometimes just to make me smile.
Shell
2
star
38

DownloadService

A Swift service for handling downloads and their associated resources.
Swift
1
star
39

SwiftHelpDemo

A demo project for showcasing SwiftHelp
Swift
1
star
40

SwiftHelp

A SwiftUI library for building interactive help systems in your apps
Swift
1
star
41

Development-Environment

All files, settings and configurations I use for my Development Environment's
1
star
42

FlowLayout

A high-performance flow layout that provides global headers, footers, section backgrounds and various configurations.
Swift
1
star
43

SPXCacheManager

A highly configurable and reusable library for handling caching in your iOS apps. Not only for images!!
Objective-C
1
star
44

SPXKeychain

My iOS Keychain Wrapper
Objective-C
1
star
45

PGTest-XCTest

Swift
1
star
46

ScaledMetric

A SwiftUI dynamic property wrapper (a back-port) that scales a numeric value. iOS, macOS, tvOS, watchOS
Swift
1
star
47

Snippex

My own personal library of reusable code that compiles and standardises some classes across iOS and OSX.
Objective-C
1
star
48

CellProvider

A generic cell provider implementation in Swift
Shell
1
star