Skip to content

Instantly share code, notes, and snippets.

@YoniTsafir
Created February 12, 2013 13:52
Show Gist options
  • Save YoniTsafir/4770019 to your computer and use it in GitHub Desktop.
Save YoniTsafir/4770019 to your computer and use it in GitHub Desktop.
A useful method to mask a view according to an alpha channel to an image. Example usage: place the masking image using IB (make it hidden), and dynamically create a view around it, that you mask using this method.
+ (void)maskView:(UIView *)targetView withImageView:(UIImageView *)maskingImageView {
CALayer *maskingLayer = [CALayer layer];
maskingLayer.frame = CGRectMake(maskingImageView.frame.origin.x - targetView.frame.origin.x,
maskingImageView.frame.origin.y - targetView.frame.origin.y,
maskingImageView.frame.size.width,
maskingImageView.frame.size.height);
maskingLayer.contents = (id)maskingImageView.image.CGImage;
targetView.layer.mask = maskingLayer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment