Search code examples
iosobjective-cuiimageviewimage-masking

Create 1 image from masked image in ios


I have a image and i mask this image with another image to make shape.
I just want to change the view background color of Masked image.
I am using [UIColor colorWithPatternImage:maskedImage];
But its not working.
Please suggest me how to merge or create Masked 2 images in 1 image so colorWithPatternImage will work.


Solution

  • UIImage *originalImage = [UIImage imageNamed:@"original.png"]; //my background image
    UIImage *maskedImage       = [UIImage imageNamed:@"maskedImage.png"]; //my masked image
    
    CGSize newSize = CGSizeMake(width, height);
    UIGraphicsBeginImageContext( newSize );
    
    
    [originalImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    
    [maskedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.6];
    
    UIImage *newMaskedBackGroundImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    

    then use this newMaskedBackGroundImage, ex. [UIColor colorWithPatternImage:newMaskedBackGroundImage];