Mask image by pattern

How can I mask / crop an image using a template like image in UIImageView

? Can anyone help me with refrains?

enter image description here

+3


source to share


1 answer


Pass two images to the next function. 1) An image for the mask and 2) an image of your 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 masked = CGImageCreateWithMask([image CGImage], mask);
        return [UIImage imageWithCGImage:masked];

    }

      

You can find the complete tutorial here .



If you want to crop an image by passing some path, take a look at the SO answer .

Hope this solves your problem.

+5


source







All Articles