9

Am creating a jigsaw puzzle game for iPhone.

Here i have to crop the image just like the below image

enter image description here

After the googling i came to know uibezier path is the best one for cropping the image in irregular shapes.

But i didn't get any code or idea to how to crop the image like above one.

j0k
  • 22,600
  • 28
  • 79
  • 90
nik
  • 2,289
  • 6
  • 37
  • 60
  • Take a look at my answer here http://stackoverflow.com/questions/13153223/how-to-crop-the-image-using-uibezierpath/15353848#15353848 – umer sufyan Sep 26 '13 at 06:55

1 Answers1

11

It will take a lot of trial and error, I did this quickly just to give you an idea of how to to create shapes using Bezier Paths. Below is example code to create a square shape that I quickly created

 UIBezierPath *aPath = [UIBezierPath bezierPath];

    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(50.0, 50.0)];
    // Draw the lines.
    [aPath addLineToPoint:CGPointMake(100.0, 50.0)];
    [aPath addLineToPoint:CGPointMake(100.0, 100.0)];
    [aPath addLineToPoint:CGPointMake(50.0, 100.0)];
    [aPath closePath];

    CAShapeLayer *square = [CAShapeLayer layer];
    square.path = aPath.CGPath;
    [self.view.layer addSublayer:square];

If I had more time, I could create the image, but did this quickly as don't have too much time. Its not too hard to use once you get your head round how the points are made. It will take you a lot of trial and error to create this shape, but use the code I provided as a basis for how to create shapes using Bezier paths. You will need to create a lot more points to end up with the shape you have in mind.

I would also recommend looking at Apples developer guide for creating irregular shapes

http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/BezierPaths/BezierPaths.html

In particular look at the "Adding Curves to Your Path" for understanding how to create curves and adding them to your image. You will need that in order to create the jigsaw puzzle piece shape you are trying to create

EDIT:

Try this method

- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView;
{
    if (![[imgView layer] mask])
        [[imgView layer] setMask:[CAShapeLayer layer]];

    [(CAShapeLayer*) [[imgView layer] mask] setPath:[clippingPath CGPath]];
}

The above method will take a bezier path and an ImageView and then apply the bezier path to that particular imageView. It will do the clipping as well. Will take a lot of trial and error I would imagine to get the shape just right, can be difficult and frustrating creating complex shapes sometimes.

Quick example of applying this code

UIBezierPath *aPath = [UIBezierPath bezierPath];
    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(0.0, 0.0)];
    // Draw the lines.
    [aPath addLineToPoint:CGPointMake(50.0, 0.0)];
    [aPath addLineToPoint:CGPointMake(50.0, 50.0)];
    [aPath addLineToPoint:CGPointMake(0.0, 50.0)];
    [aPath closePath];

    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar.png"]];
    imgView.frame = CGRectMake(0, 0, 100, 100);
    [self setClippingPath:aPath :imgView];
    [self.view addSubview:imgView];

Just quickly made a square out of the top left part of the image. If you for example, had a square image, you could loop through the width and height of the image, cropping into separate squares using above code and returning them individually. Creating jigsaw puzzle piece a lot more complicated, but hope this helps

AdamM
  • 4,400
  • 5
  • 49
  • 95
  • thanks a lot for your response and spending time with my question its really appreciable. Yes now am clear in things in which path i should travel and here my another question is cropping the image. do you have any idea on that ? cropping with plotted curve using this uibezier path? – nik Oct 01 '12 at 10:30
  • And i just want to match this pattern with uiimage and i just want to crop the image with that pattern – nik Oct 01 '12 at 10:36
  • 1
    See my edit, I included a method that you can use where it will take bezier path and an imageView and do the clipping for you. Hope that helps – AdamM Oct 01 '12 at 14:10
  • Its awesome. works fine. thanks a lot. This so simple and but think i need to work a lot in cropping the image in specified shape bezier path. – nik Oct 03 '12 at 06:28
  • Yeah it can be difficult to crop the shape just right, just takes time good luck with it!! – AdamM Oct 03 '12 at 07:14
  • Can I suggest PaintCode to create the path. – Cyrille May 10 '13 at 10:39
  • I was stuck and spend almost an hour on this one! Thanks – Gokul Sep 17 '16 at 16:20