1

I have two Images 1.Original image 2.mask image

I want to mask original image with mask image and it will done using below code.

- (UIImage *) maskImage:(UIImage *)originalImage : (NSString *)maskImageName
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    UIImage *maskImage = [UIImage imageNamed:maskImageName];
    CGImageRef maskImageRef = [maskImage CGImage];

    CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);//kCGImageAlphaPremultipliedLast
    CGColorSpaceRelease(colorSpace);
    if (mainViewContentContext == NULL)
    {
        return NULL;
    }

    CGFloat ratio = 0;
    ratio = maskImage.size.width/ originalImage.size.width;
    if(ratio * originalImage.size.height < maskImage.size.height)
    {
        ratio = maskImage.size.height/ originalImage.size.height;
    }
    CGRect rect1  = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
    CGRect rect2  = {{-((originalImage.size.width*ratio)-maskImage.size.width)/2 , -((originalImage.size.height*ratio)-maskImage.size.height)/2}, {originalImage.size.width*ratio, originalImage.size.height*ratio}};

    CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);//rect1
    CGContextDrawImage(mainViewContentContext, rect2, originalImage.CGImage);//rect2

    CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
    CGContextRelease(mainViewContentContext);

    UIImage *theImage = [UIImage imageWithCGImage:newImage];
    CGImageRelease(newImage);
     CGImageRelease(maskImageRef);
    CGColorSpaceRelease(colorSpace);

    return theImage;
}

But when this method masking images than original image resize with mask shape

My requirement is

original image zoom,move,rotate using gesture

after than i want masking

and in masking original image not resize If (Modified or not modified) only put mask shape and other transparent portion crop.

enter image description here

want to mask only color portion from image other portion will be transparent so corp original image with only colored portion of mask image and other portion will be crop not and when original image zoom than that zoomed portion will be covered with mask not original image.

JAY RAPARKA
  • 1,353
  • 2
  • 13
  • 33

0 Answers0