3

How to mask/crop a picture using the pattern like the image in UIImageView? Can any one help me with refrences?

enter image description here

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
Dev_iOS
  • 251
  • 1
  • 2
  • 8

1 Answers1

5

Pass two images to the below function. 1) Image to be mask and 2) your mask pattern 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 masked = CGImageCreateWithMask([image CGImage], mask);
        return [UIImage imageWithCGImage:masked];

    }

You can refer the full tutorial here.

If you want to clip image by passing some path then have a look at this SO Answer.

Hope this will solve your problem.

Community
  • 1
  • 1
Moin Ahmed
  • 2,898
  • 21
  • 33