Masking and Reverse Masking in Imageview ios

I have one Imageview that contains the Image . And one Mask Shape that contains shape of rabbit.
I have one code that gives the below result.

- (UIImage*)mynewmaskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGImageRef maskImageRef = [maskImage CGImage];

// create a bitmap graphics context the size of the image
CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL,320, 380, 8, 0, colorSpace,(CGBitmapInfo) kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);

if (mainViewContentContext==NULL)
    return NULL;

CGFloat ratio = 0;

ratio = 320/ image.size.width;

if(ratio * image.size.height < 380) {
    ratio = 380/ image.size.height;
}

CGRect rect1  = {{0, 0}, {320,380}};
CGRect rect2  = {{-((image.size.width*ratio)-320)/2 , -((image.size.height*ratio)-380)/2}, {image.size.width*ratio, image.size.height*ratio}};

//  CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);

CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);
CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);

UIImage *theImage = [UIImage imageWithCGImage:newImage];

CGImageRelease(newImage);

// return the image
return theImage;}

The above code gives this result.
enter image description here

  • Building a Cocoapod with Swift and dependency on Objective-C framework
  • SceneKit Orbital Transformations
  • WKWebView Persistent Storage of Cookies
  • How to uninstall PhoneGap from my project and machine
  • GCM, Android, iOS, using the same Sender id and Server key/authorization key
  • Set iPhone date/time within app for testing purposes?
  • But I want the below result (like reverse masking).
    enter image description here

    How it is possible.Please Help me.
    Thanks.

    Solutions Collect From Internet About “Masking and Reverse Masking in Imageview ios”

    You should look through blendMode. Try smth like this:

    [rabbitImage drawInRect:rect
                  blendMode:kCGBlendModeDestinationOut
                    alpha:1.0];