Graphics Reference
In-Depth Information
Figure 4.12
Combining separate image and mask layers to create a masked image
To demonstrate this, let's create a simple project that masks one image with another using
the layer
mask
property. To simplify things, we'll create our image layer in Interface
Builder using a
UIImageView
, so that only the
mask
layer needs to be created and
applied programmatically. Listing 4.5 shows the code to do this, and Figure 4.13 shows the
result.
Listing 4.5
Applying a Layer Mask
@interface
ViewController ()
@property
(
nonatomic
,
weak
)
IBOutlet
UIImageView
*imageView;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//create mask layer
CALayer
*maskLayer = [
CALayer
layer
];
maskLayer.
frame
=
self
.layerView.
bounds
;
UIImage
*maskImage = [
UIImage
imageNamed
:
@"Cone.png"
];
maskLayer.
contents
= (
__bridge
id
)maskImage.
CGImage
;
//apply mask to image layer