Question: I'am trying to create profile picture image with custom shape pattern with using masking in ios. Here is what i've used to create masked image : 

```
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

    CGImageRef maskRef = maskImage.CGImage;

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);

    CGImageRef maskedImageRef = CGImageCreateWithMask([image CGImage], mask);
    UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];

    CGImageRelease(mask);
    CGImageRelease(maskedImageRef);

    // returns new image with mask applied
    return maskedImage;
}

```

After implementing instance , i am calling : \*(\_profileImage is global UIImage)

```
UIImage *maskImage = [UIImage imageNamed:@"maskProfilePicture.png"];

UIImage *maskedImage = [self maskImage:_profileImage withMask:maskImage];

```

and result (Image should be added in subview of mask. But it seems , mask image overlap on Image that need to be masked ): 

![masked image](https://i.stack.imgur.com/I0MQi.png)

The mask image : (Maybe mask image properties are wrong that i created in photoshop :)

![Mask Image](https://i.stack.imgur.com/wTA3P.png)

Answer: 
The Core Graphics calls look correct.

Change the mask image to be have a black where you want the image to show through, and white elsewhere.

![mask image](https://i.stack.imgur.com/DNoCW.png)
You can use CALayer's mask property to achieve the result.

[CALayer Apple Documentation](https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/mask)

Your masking layer should be transparent in places where you want to cut out the image.