GitHub - Harley-xk/MaLiang: iOS painting framework based on OpenGL ES. 神笔马良...

 4 years ago
source link: https://github.com/Harley-xk/MaLiang
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.



CI Status Version License Platform

MaLiang is a painting framework based on OpenGL ES. The name of "MaLiang" comes from a boy who had a magical brush in Chinese ancient fairy story.

Simplified Chinese



iOS 9.0+, Swift 4.1+

The core painting module is based on OpenGL ES 3.0

You can simply make it compatible with lower version of iOS and swift by changing only serval lines of code.


MaLiang is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'MaLiang'


MaLiang is simple to use.

  1. import MaLiang
  2. enjoy painting!


open class Canvas: MLView

A Canvas is the basic component of MaLiang. You will paint all things on it. Canvas extends from MLView, whitch extends from UIView. MLView handles all the logic with OpenGL and hides them from you.

Canvas can be simply created with xib or code.

  • with xib or storyboard, simply drag and drop an UIView object into your view controller and change it's class to Canvas and module to MaLiang
  • with code, just create with init(frame:) as any UIView you do before.

Now, all things necessary is done!


You can take snapshot on canvas now. Just call snapshot function on Canvas and you will get an optional UIImage object.


With all things done, you can do more with Brush!

Brush is the key feature to MaLiang. It holds textures and colors, whitch makes it possiable to paint amazing things.

Brush can be created with a image:

let image = UIImage(named: "pencil.png")
let pencil = Brush(texture: image)
canvas.brush = pencil

Brush have serval properties for you to custmize:

// opacity of texture, affects the darkness of stroke
// set opacity to 1 may cause heavy aliasing
open var opacity: CGFloat = 0.3

// width of stroke line in points
open var pointSize: CGFloat = 4

// this property defines the minimum distance (measureed in points) of nearest two textures
// defaults to 1, this means erery texture calculated will be rendered, dictance calculation will be skiped
open var pointStep: CGFloat = 1

// sensitive of pointsize changed from force, from 0 - 1
open var forceSensitive: CGFloat = 0

/// color of stroke
open var color: UIColor = .black

With all these properties, you can create you own brush as your imagination.

Force & 3D Touch

MaLiang supports automatically adjustment if stroke size with painting force. 3D Touch is supported by default, and simulated force will be setup on devices those are not supporting this.

forceSensitive is the property that force affects the storke size. It should be set between 0 to 1. the smaller the value is, the less sensitive will be. if sets to 0, then force will not affects the stroke size.


Document is not required. It holds all the data on the Canvas, and makes the undo and redo actions to be possiable.
And you can implement your own saving logic with the data holds by Document.

To enable the Document for Canvas, there needs only one line of code:


The operation above may be failed if there's not enough disk rooms. Because we need rooms to keep textures and painting datas on disk.
Use do-catch to process the error when it appears:

do {
    try canvas.setupDocument()
} catch {
    // do somthing when error occurs


  • Undo & Redo
  • Export to image
  • Text element
  • Image element
  • Texture rotation


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

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK