Skip to content

huang-kun/YJLayerBasedMasking

Repository files navigation

YJLayerBasedMasking

CI Status Version Carthage compatible License Platform

Why layer based masking ?

Using YJLayerBasedMasking to get circular or rounded corner image is simple, light-weight, without extra cost for blended layer and offscreen-rendering.

Here is a demo using a JPEG format image without alpha channel. To display it as circular or rounded corner effect, we can set layer's properties directly or using YJLayerBasedMasking.

Get same results by setting layer.masksToBounds (middle row) or using YJLayerBasedMasking (last row)

effect

Color blended layers testing (Green color means good, red color means layer-blending cost)

blended layer

Color offscreen-rendering testing (Yellow color means offscreen-rendering cost)

offscreen-rendered

How to use it ?

To display a circular UIImageView instance, just call this.

// circular imageView
[imageView yj_addCircularShapeLayerWithColor:backgroundColor];
// circular imageView with border
[imageView yj_addCircularShapeLayerWithColor:backgroundColor borderWidth:borderWidth borderColor:borderColor];

To display a rounded corner UIImageView instance, just call this.

// rounded corner imageView
[imageView yj_addRoundedCornerShapeLayerWithColor:backgroundColor cornerRadius:cornerRadius];
// rounded corner imageView with border
[imageView yj_addRoundedCornerShapeLayerWithColor:backgroundColor cornerRadius:cornerRadius borderWidth:borderWidth borderColor:borderColor];

Notice

  1. This is not good for display rounded corner with transparency background.
  2. This is not good for any view with its size changing constantly.
  3. This is not good for any view with superview's background color changing constantly. (e.g. When user tap on UITableViewCell, it's background color becomes gray. To make the YJLayerBasedMasking working, either you can set cell's property to UITableViewCellSelectionStyleNone or change shape layer's color to match the new background color)

If you need to display a circular image effect with a good performance (e.g. display user's avatar image), then try YJLayerBasedMasking.

Requirements

iOS 7+

Installation

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

pod "YJLayerBasedMasking"

For iOS 8+, it also support Carthage. To install, add the following line to your Cartfile:

github "huang-kun/YJLayerBasedMasking"

Author

huang-kun, jack-huang-developer@foxmail.com

License

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

About

A light-weight and high-performance approach to get circular or rounded corner UIImageView.

Resources

License

Stars

Watchers

Forks

Packages

No packages published