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)
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];
- This is not good for display rounded corner with transparency background.
- This is not good for any view with its size changing constantly.
- 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.
iOS 7+
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"
huang-kun, [email protected]
YJLayerBasedMasking is available under the MIT license. See the LICENSE file for more info.