Tuesday, July 30, 2013

Masking Image with a transparent PNG

Hi i have made a function in which you can pass 2 UIImages and it will mask with the transparent Image

- (UIImageView*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CALayer *mask1 = [CALayer layer];
    mask1.contents = (id)[maskImage CGImage];
    mask1.frame = CGRectMake(0, 0, 1024, 768);
    imageView.layer.mask = mask1;
    imageView.layer.masksToBounds = YES;
    return imageView;

Its returns the UIImageView, which you can add directly in UIView. You can call this method like following:

    UIImage *image = [UIImage imageNamed:@"screenFull.png"];
    UIImage *mask = [UIImage imageNamed:@"screen_mask.png"];
    [self.completeSingleView addSubview:[self maskImage:image withMask:mask]];

Here completeSingleView is a UIView.

Reference is taken from