Custom Image Mask in iOS

I have a problem with masking images. I play a puzzle and must make custom images. I found and tried two ways to custom crop:

  • Use of property CALayer.mask.
  • Use of property UIImage.mask.

In the first case, I create my own path, and then assigns it to the property CAShapeLayer.path, and then assigns the property CAShapeLayerto CALayer.mask. At the end, I have a custom cropped image. In the second option, I use the method first CGImageMaskCreate()(I use the previously created images of the black puzzle mask), then CGContextClipToMask(). In both cases, I have a performance problem (mainly when I crop the image into 16 puzzles and drag it around the screen).

Are there any other approaches to crop the image in my own way. (I do not know how to solve the performance problem). Thanks in advance.

+5
source share
3 answers

:

-(UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return cropped;
}

...

UIImage *temp_image = [self imageByCropping:original_image toRect:clipping_rectangle];
0

Maybe you should think about how to draw an image in a new image with alpha backgrounds, redrawing the current background. I mean: the whole pixel that is inside the puzzle piece: normal color, all pixels behind the puzzle piece = transparent. And then try mixing it with a new background or cross it out.

Only my 2 cents. :)

0
source

All Articles