Custom image mask in iOS
Asked Answered
H

3

5

I have a issue with masking images. I do game "puzzle" and have to make custom images. I found and tried 2 way of custom cropping:

  1. Using CALayer.mask property.
  2. Using UIImage.mask property.

In first option i create my custom path, then assign it to CAShapeLayer.path property, then assign CAShapeLayer to CALayer.mask property. At the end i have custom cropped image. In second option i use firstly use CGImageMaskCreate() method (i use previously created black mask images of puzzle), then CGContextClipToMask(). In either options i have problem with performance (mostly when i crop image into 16 puzzles and drag in over the screen).

Is there any other approaches to crop image in custom way. (I don't know how to solve performance problem). Thanks in advance.

Haggadah answered 4/5, 2011 at 8:25 Comment(1)
What you are asking about is NOT cropping, it's called masking. You have repeatedly stated that you want to mask non-rectangular regions; cropping almost always refers to rectangular regions. You may want to adjust the question accordingly.Cherry
D
6

There are lots of UIImage-categories out there you can use for this. Give me a moment and I'll post some links here:

Dull answered 4/5, 2011 at 8:27 Comment(1)
Thanks for answer, Tim van Elsloo. But this links consists manuals about cropping a rectangle image. But i need a custom. Not rectangle. Like this one: imageshack.us/f/856/hard131.pngHaggadah
A
0

Try this:

-(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];
Adcock answered 4/5, 2011 at 16:9 Comment(1)
Thanks for answer, Martin. But your source code can only cropping rectangle regions. But i not need it. I need to crop rectangle image into this one: imageshack.us/f/856/hard131.png.Haggadah
A
0

Maybe you should consider about drawing the image in a new image with an alpha background an overdrawing the current background. I mean: All pixel which are inside the jigsaw piece: normal colour, all pixels outside the jigsaw piece = transparent. And then try to blend it to the new background or overdrawing it.

Just my 2 cents. :)

Acupuncture answered 12/3, 2012 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.