How can I mask a UIImageView? How can I mask a UIImageView? ios ios

How can I mask a UIImageView?


There's an easier way.

#import <QuartzCore/QuartzCore.h>// remember to include Framework as wellCALayer *mask = [CALayer layer];mask.contents = (id)[[UIImage imageNamed:@"mask.png"] CGImage];mask.frame = CGRectMake(0, 0, <img_width>, <img_height>);yourImageView.layer.mask = mask;yourImageView.layer.masksToBounds = YES;

For Swift 4 and plus follow code below

let mask = CALayer()mask.contents =  [ UIImage(named: "right_challenge_bg")?.cgImage] as Anymask.frame = CGRect(x: 0, y: 0, width: leftBGImage.frame.size.width, height: leftBGImage.frame.size.height)leftBGImage.layer.mask = maskleftBGImage.layer.masksToBounds = true


The tutorial uses this method with two parameters: image and maskImage, these you have to set when you call the method. An example call could look like this, assuming the method is in the same class and the pictures are in your bundle:

Note - amazingly the images do not even have to be the same size.

...UIImage *image = [UIImage imageNamed:@"dogs.png"];UIImage *mask = [UIImage imageNamed:@"mask.png"];// result of the masking methodUIImage *maskedImage = [self maskImage:image withMask:mask];...- (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 you provided your code I have added some numbers as comments to it for reference. You still have two options. This whole thing is a method, which you are calling somewhere. You don't need to create the images inside it: this reduces the reusability of the method to zero.

To get your code working. Change the methods head (1.) to

- (UIImage *)maskImageMyImages {

Then change the name of the variable in 2. to

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

The method will return your masked images so you'll have to call this method in some place.Can you show us the code where you are calling your method?


Swift 3 - Simplest Solution I Found

I created an @IBDesignable for this so you could see the effect right away on your storyboard.

import UIKit@IBDesignableclass UIImageViewWithMask: UIImageView {    var maskImageView = UIImageView()    @IBInspectable    var maskImage: UIImage? {        didSet {            maskImageView.image = maskImage            updateView()        }    }    // This updates mask size when changing device orientation (portrait/landscape)    override func layoutSubviews() {        super.layoutSubviews()        updateView()    }    func updateView() {        if maskImageView.image != nil {            maskImageView.frame = bounds            mask = maskImageView        }    }} 

How to use

  1. Create a new file and paste in that code above.
  2. Add UIImageView to your storyboard (assign an image if you want).
  3. On Identity Inspector: Change the custom class to "UIImageViewWithMask" (custom class name above).
  4. On Attributes Inspector: Select mask image youwant to use.

Example

Masking on Storyboard

Notes

  • Your mask image should have a transparent background (PNG) for the non-black part.